Using WordPress ‘get_users_drafts’ PHP filter

The get_users_drafts WordPress PHP filter allows you to modify the user’s drafts query string.

Usage

add_filter('get_users_drafts', 'my_custom_get_users_drafts', 10, 1);
function my_custom_get_users_drafts($query) {
  // your custom code here
  return $query;
}

Parameters

  • $query (string) – The user’s drafts query string.

More information

See WordPress Developer Resources: get_users_drafts

Examples

Limit the number of drafts returned

Limit the number of user drafts returned to 5.

function limit_users_drafts($query) {
  $query .= " LIMIT 5";
  return $query;
}
add_filter('get_users_drafts', 'limit_users_drafts', 10, 1);

Order drafts by title

Order user drafts by title in ascending order.

function order_users_drafts_by_title($query) {
  $query .= " ORDER BY post_title ASC";
  return $query;
}
add_filter('get_users_drafts', 'order_users_drafts_by_title', 10, 1);

Include only specific post types

Include only ‘page’ post types in the user drafts.

function include_specific_post_types($query) {
  $query .= " AND post_type = 'page'";
  return $query;
}
add_filter('get_users_drafts', 'include_specific_post_types', 10, 1);

Exclude specific post types

Exclude ‘attachment’ post types from the user drafts.

function exclude_specific_post_types($query) {
  $query .= " AND post_type != 'attachment'";
  return $query;
}
add_filter('get_users_drafts', 'exclude_specific_post_types', 10, 1);

Include drafts from a specific date

Include only user drafts created after a specific date.

function include_drafts_from_date($query) {
  $date = '2023-01-01';
  $query .= " AND post_date >= '$date'";
  return $query;
}
add_filter('get_users_drafts', 'include_drafts_from_date', 10, 1);