Using WordPress ‘get_others_unpublished_posts()’ PHP function

The get_others_unpublished_posts() WordPress PHP function retrieves editable posts from other users, excluding the specified user.

Usage

get_others_unpublished_posts($user_id, $type)

Custom Example:

$other_users_posts = get_others_unpublished_posts(1, 'draft');

Parameters

  • $user_id (int): User ID to exclude from retrieving posts.
  • $type (string): Post type to retrieve. Accepts ‘draft’, ‘pending’, or ‘any’ (all). Default is ‘any’.

More information

See WordPress Developer Resources: get_others_unpublished_posts()

Examples

Get Drafts from Other Users

Retrieve all draft posts from other users, excluding the user with the ID of 2.

$other_users_drafts = get_others_unpublished_posts(2, 'draft');

Get Pending Posts from Other Users

Retrieve all pending posts from other users, excluding the user with the ID of 3.

$other_users_pending = get_others_unpublished_posts(3, 'pending');

Get All Unpublished Posts from Other Users

Retrieve all unpublished posts (draft and pending) from other users, excluding the user with the ID of 4.

$other_users_unpublished = get_others_unpublished_posts(4, 'any');

Display Other Users’ Unpublished Posts

Display the titles of unpublished posts from other users, excluding the user with the ID of 5.

$other_users_unpublished = get_others_unpublished_posts(5, 'any');

foreach ($other_users_unpublished as $post) {
    echo '<h2>' . $post->post_title . '</h2>';
}

Get Unpublished Posts of a Specific Post Type

Retrieve all unpublished posts of a custom post type ‘products’ from other users, excluding the user with the ID of 6.

add_filter('posts_where', 'filter_post_type_products');
$other_users_unpublished_products = get_others_unpublished_posts(6, 'any');
remove_filter('posts_where', 'filter_post_type_products');

function filter_post_type_products($where) {
    global $wpdb;
    $where .= " AND {$wpdb->posts}.post_type = 'products'";
    return $where;
}