Using WordPress ‘get_post_statuses()’ PHP function

The get_post_statuses() WordPress PHP function retrieves all of the supported post statuses in WordPress.

Usage

To use the function, simply call get_post_statuses(). The function returns an array of post statuses.

$post_statuses = get_post_statuses();
print_r($post_statuses);

Parameters

  • None

More information

See WordPress Developer Resources: get_post_statuses

This function returns a hard-coded list of statuses. To see the list of all post statuses available in your WordPress instance, including custom statuses, use get_post_stati.

Examples

Display all post statuses

This code displays a list of all available post statuses.

$post_statuses = get_post_statuses();
foreach ($post_statuses as $status => $description) {
    echo "Status: {$status}, Description: {$description}\n";
}

Check if a specific post status exists

This code checks if the ‘draft’ post status is available in WordPress.

$post_statuses = get_post_statuses();
if (array_key_exists('draft', $post_statuses)) {
    echo "Draft status exists!";
} else {
    echo "Draft status not found.";
}

Count the number of post statuses

This code counts the total number of post statuses available in WordPress.

$post_statuses = get_post_statuses();
$total_statuses = count($post_statuses);
echo "There are {$total_statuses} post statuses available.";

Filter posts by a specific post status

This code retrieves and displays posts with the ‘draft’ post status.

$args = array(
    'post_type' => 'post',
    'post_status' => 'draft',
);
$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        echo "Title: " . get_the_title() . "\n";
    }
} else {
    echo "No draft posts found.";
}
wp_reset_postdata();

Get the description of a specific post status

This code retrieves the description of the ‘private’ post status.

$post_statuses = get_post_statuses();
if (array_key_exists('private', $post_statuses)) {
    echo "The private post status description is: {$post_statuses['private']}";
} else {
    echo "Private post status not found.";
}