Using WordPress ‘get_available_post_statuses()’ PHP function

The get_available_post_statuses() WordPress PHP function returns all the possible statuses for a post type.

Usage

get_available_post_statuses($type);

Custom Example

$statuses = get_available_post_statuses('post');
print_r($statuses);

Output

Array
(
    [0] => publish
    [1] => future
    [2] => draft
    [3] => pending
    [4] => private
    [5] => trash
    [6] => auto-draft
    [7] => inherit
)

Parameters

  • $type (string) Optional: The post_type you want the statuses for. Default is ‘post’.

More information

See WordPress Developer Resources: get_available_post_statuses()

Examples

Display All Post Statuses for Custom Post Type

This example retrieves all post statuses for a custom post type named “events”.

$statuses = get_available_post_statuses('events');
foreach($statuses as $status) {
    echo $status . '<br>';
}

Check if a Specific Status is Available for a Post Type

This example checks if the ‘draft’ status is available for the ‘post’ post type.

$statuses = get_available_post_statuses('post');
if(in_array('draft', $statuses)) {
    echo "Draft status is available.";
} else {
    echo "Draft status is not available.";
}

Count the Number of Post Statuses for a Post Type

This example counts the number of available post statuses for the ‘page’ post type.

$statuses = get_available_post_statuses('page');
$status_count = count($statuses);
echo "There are {$status_count} post statuses available for 'page' post type.";

Add a Prefix to Each Post Status

This example adds a prefix “Status: ” to each post status for the ‘post’ post type and displays them.

$statuses = get_available_post_statuses('post');
foreach($statuses as $status) {
    echo "Status: {$status}" . '<br>';
}

Convert Post Statuses to a List

This example converts the post statuses for the ‘post’ post type into an HTML list.

$statuses = get_available_post_statuses('post');
echo '<ul>';
foreach($statuses as $status) {
    echo "<li>{$status}</li>";
}
echo '</ul>';