Using WordPress ‘get_others_drafts()’ PHP function

The get_others_drafts() WordPress PHP function retrieves drafts from other users.

Usage

get_others_drafts( $user_id );

Example:

// Get drafts of user with ID 5
$other_users_drafts = get_others_drafts(5);
print_r($other_users_drafts);

Parameters

  • $user_id (int) – Required. The user ID for which you want to retrieve the drafts.

More information

See WordPress Developer Resources: get_others_drafts()

Examples

Display list of other user’s draft titles

This example retrieves and displays the titles of the drafts of user with ID 3.

$user_id = 3;
$drafts = get_others_drafts($user_id);
echo "Drafts of user with ID {$user_id}:\n";
foreach ($drafts as $draft) {
    echo "- {$draft->post_title}\n";
}

Count the number of drafts by another user

This example counts the number of drafts created by the user with ID 7.

$user_id = 7;
$drafts = get_others_drafts($user_id);
$count = count($drafts);
echo "User with ID {$user_id} has {$count} drafts.\n";

This example retrieves the drafts of user with ID 8 and displays them with edit links.

$user_id = 8;
$drafts = get_others_drafts($user_id);
echo "Edit drafts of user with ID {$user_id}:\n";
foreach ($drafts as $draft) {
    $edit_link = get_edit_post_link($draft->ID);
    echo "<a href='{$edit_link}'>{$draft->post_title}</a>\n";
}

Retrieve drafts from multiple users

This example retrieves drafts from users with IDs 2 and 6, then displays the titles.

$user_ids = [2, 6];
foreach ($user_ids as $user_id) {
    $drafts = get_others_drafts($user_id);
    echo "Drafts of user with ID {$user_id}:\n";
    foreach ($drafts as $draft) {
        echo "- {$draft->post_title}\n";
    }
}

Display other user’s drafts with their creation date

This example retrieves the drafts of user with ID 4 and displays them with their creation date.

$user_id = 4;
$drafts = get_others_drafts($user_id);
echo "Drafts of user with ID {$user_id}:\n";
foreach ($drafts as $draft) {
    $date = date('Y-m-d', strtotime($draft->post_date));
    echo "- {$draft->post_title} ({$date})\n";
}