apply_filters( ‘widget_posts_args’, array $args, array $instance )

Filters the arguments for the Recent Posts widget.

Description

See also

Parameters

$argsarray
An array of arguments used to retrieve the recent posts.
$instancearray
Array of settings for the current widget.

More Information

Use any of the WP_Query parameters for $args.

Source

apply_filters(
	'widget_posts_args',
	array(
		'posts_per_page'      => $number,
		'no_found_rows'       => true,
		'post_status'         => 'publish',
		'ignore_sticky_posts' => true,
	),
	$instance
)

Changelog

VersionDescription
4.9.0Added the $instance parameter.
3.4.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Adding to my custom theme in functions.php:

    //filter recent posts widgets by category name
    function myorg_recentposts_events($args, $instance) {
    	$args['category_name'] = 'upcoming-events';
    	return $args;
    }
    add_filter('widget_posts_args', 'myorg_recentposts_events', 1, 2);

    Most important, return $args!! Spent an hour trying to figure out why my filter wasn’t working.

  2. Skip to note 4 content

    Example migrated from Codex:

    Use the following to sort recent posts of the widget by date. Add the code to the functions.php file of the child theme.

    add_filter('widget_posts_args', 'filter_recent_posts_widget_parameters'); 
    
    function filter_recent_posts_widget_parameters($args, $instance) {
       $args['orderby'] = 'date';
       return $args;
    }

You must log in before being able to contribute a note or feedback.