Using WordPress ‘get_others_drafts’ PHP filter

The get_others_drafts WordPress PHP filter allows you to modify the query used to retrieve drafts created by other users.

Usage

add_filter('get_others_drafts', 'your_custom_function');
function your_custom_function($query) {
  // your custom code here
  return $query;
}

Parameters

None

More information

See WordPress Developer Resources: get_others_drafts

Examples

Exclude drafts from a specific user

Exclude drafts created by user with ID 5.

add_filter('get_others_drafts', 'exclude_specific_user_drafts');
function exclude_specific_user_drafts($query) {
  $query .= " AND post_author != 5";
  return $query;
}

Limit the number of drafts

Limit the number of drafts retrieved to 10.

add_filter('get_others_drafts', 'limit_drafts');
function limit_drafts($query) {
  $query .= " LIMIT 10";
  return $query;
}

Order drafts by title

Order drafts by title in ascending order.

add_filter('get_others_drafts', 'order_drafts_by_title');
function order_drafts_by_title($query) {
  $query .= " ORDER BY post_title ASC";
  return $query;
}

Filter drafts by category

Show only drafts from a specific category with ID 3.

add_filter('get_others_drafts', 'filter_drafts_by_category');
function filter_drafts_by_category($query) {
  global $wpdb;
  $query .= " AND ID IN (
    SELECT object_id
    FROM {$wpdb->term_relationships}
    WHERE term_taxonomy_id = 3
  )";
  return $query;
}

Filter drafts by custom field

Show only drafts with a custom field named “featured” and value “yes”.

add_filter('get_others_drafts', 'filter_drafts_by_custom_field');
function filter_drafts_by_custom_field($query) {
  global $wpdb;
  $query .= " AND ID IN (
    SELECT post_id
    FROM {$wpdb->postmeta}
    WHERE meta_key = 'featured'
    AND meta_value = 'yes'
  )";
  return $query;
}