I’d like to use the WP Recent Posts widget but set it up to order by the post’s modified date.
I found this snippet online and tried it:
add_filter('widget_posts_args', 'filter_recent_posts_widget_parameters');
function filter_recent_posts_widget_parameters($args, $instance) {
$args['orderby'] = 'modified';
return $args;
}
however, the widget disappears from the sidebar when I do that. I’m getting an error (using wp debug):
Fatal error: Uncaught ArgumentCountError: Too few arguments to function filter_recent_posts_widget_parameters(), 1 passed in /home/rev/public_html/wp-includes/class-wp-hook.php on line 326 and exactly 2 expected in /home/rev/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(581) : eval()'d code:3 Stack trace: #0 /home/rev/public_html/wp-includes/class-wp-hook.php(326): filter_recent_posts_widget_parameters() #1 /home/rev/public_html/wp-includes/plugin.php(205): WP_Hook->apply_filters() #2 /home/rev/public_html/wp-includes/widgets/class-wp-widget-recent-posts.php(73): apply_filters() #3 /home/rev/public_html/wp-includes/class-wp-widget.php(394): WP_Widget_Recent_Posts->widget() #4 /home/rev/public_html/wp-includes/widgets.php(837): WP_Widget->display_callback() #5 /home/rev/public_html/wp-content/themes/generatepress/sidebar.php(22): dynamic_sidebar() #6 /home/rev/public_html/wp-includes/template.php(790): require_once('...') #7 /home/rev/public_html/wp-includes/template.php(725): load_template() #8 /home/rev/public_html/wp-includes/general-template.php(136): locate_template() #9 /home/rev/public_html/wp-content/themes/generatepress/inc/structure/sidebars.php(34): get_sidebar() #10 /home/rev/public_html/wp-content/themes/generatepress/page.php(57): generate_construct_sidebars() #11 /home/rev/public_html/wp-includes/template-loader.php(106): include('...') #12 /home/rev/public_html/wp-blog-header.php(19): require_once('...') #13 /home/rev/public_html/index.php(17): require('...') #14 {main} thrown in /home/rev/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(581) : eval()'d code on line 3
The recent posts widget works if I don’t use this snippet (though ordered by publication date). How can I adjust this code so that it works with the order by modified date?
Thanks,
Chris
2
Answers
To fix this issue, can you please modify your code to ensure that the function can handle both arguments correctly. Like this;
Try using this updated code, and it should resolve the "Too arguments" error you were encountering.
Thanks
The error you’re encountering, "ArgumentCountError: Too few arguments," suggests that your filter function is missing the expected number of arguments. The widget_posts_args filter expects two parameters to be passed to the callback function: $args and $instance.
Here’s the corrected code:
In the add_filter function, the last parameter (2) specifies the number of parameters that the callback function (filter_recent_posts_widget_parameters) expects & Error is generated because that parameter missing.