Using WordPress ‘dashboard_recent_posts_query_args’ PHP filter

The dashboard_recent_posts_query_args WordPress PHP filter allows you to modify the query arguments used for the Recent Posts widget on the dashboard.

Usage

add_filter('dashboard_recent_posts_query_args', 'custom_dashboard_recent_posts_query_args');

function custom_dashboard_recent_posts_query_args($query_args) {
  // your custom code here
  return $query_args;
}

Parameters

  • $query_args (array): The arguments passed to WP_Query to produce the list of posts.

More information

See WordPress Developer Resources: dashboard_recent_posts_query_args

Examples

Change the number of recent posts displayed

Modify the number of posts displayed in the Recent Posts widget to 10.

add_filter('dashboard_recent_posts_query_args', 'custom_post_count_recent_posts_widget');

function custom_post_count_recent_posts_widget($query_args) {
  $query_args['posts_per_page'] = 10;
  return $query_args;
}

Display only posts from a specific category

Display only posts from the category with the slug “news” in the Recent Posts widget.

add_filter('dashboard_recent_posts_query_args', 'custom_category_recent_posts_widget');

function custom_category_recent_posts_widget($query_args) {
  $query_args['category_name'] = 'news';
  return $query_args;
}

Exclude posts from a specific category

Exclude posts from the category with the slug “internal” in the Recent Posts widget.

add_filter('dashboard_recent_posts_query_args', 'exclude_category_recent_posts_widget');

function exclude_category_recent_posts_widget($query_args) {
  $query_args['category__not_in'] = array(get_cat_ID('internal'));
  return $query_args;
}

Order recent posts by title

Order the posts in the Recent Posts widget by their title in ascending order.

add_filter('dashboard_recent_posts_query_args', 'order_title_recent_posts_widget');

function order_title_recent_posts_widget($query_args) {
  $query_args['orderby'] = 'title';
  $query_args['order'] = 'ASC';
  return $query_args;
}

Show only published posts

Display only published posts in the Recent Posts widget.

add_filter('dashboard_recent_posts_query_args', 'published_posts_recent_posts_widget');

function published_posts_recent_posts_widget($query_args) {
  $query_args['post_status'] = 'publish';
  return $query_args;
}