Using WordPress ‘get_post_status()’ PHP function

The get_post_status() WordPress PHP function retrieves the post status based on the post ID.

Usage

get_post_status( $post );

Custom Example

$status = get_post_status( 42 );
echo $status; // Output: 'publish'

Parameters

  • $post (int|WP_Post) (Optional) – Post ID or post object. Defaults to global $post. Default: null

More information

See WordPress Developer Resources: get_post_status()

Possible return values: publish, future, draft, pending, private, trash, auto-draft, inherit, or custom status types introduced by plugins.

Examples

Check if Post is Private

if (get_post_status($ID) == 'private') {
    echo 'private';
} else {
    echo 'public';
}

Display Post Status with Error Message

$current_status = get_post_status($postID);
$err_msg = '';

if (false !== $current_status) {
    if ('publish' === $current_status) {
        $err_msg = esc_html__('Post status is set to: Publish.', 'textdomain');
    } elseif ('draft' === $current_status) {
        $err_msg = esc_html__('Your post is in Draft mode.', 'textdomain');
    }
} else {
    $err_msg = esc_html__('Sorry, the post status is not available at this time.', 'textdomain');
}

echo $err_msg;

Get Localized Label for Post Status

echo get_post_status_object(get_post_status())->label;

Change Post Status if Draft

$post_status = get_post_status($post_id);

if ('draft' === $post_status) {
    wp_update_post(array(
        'ID' => $post_id,
        'post_status' => 'publish'
    ));
}

Count Posts with Specific Status

function count_posts_by_status($status) {
    $args = array(
        'post_type' => 'post',
        'post_status' => $status,
        'posts_per_page' => -1,
    );

    $query = new WP_Query($args);

    return $query->post_count;
}

$draft_posts_count = count_posts_by_status('draft');
echo 'Number of draft posts: ' . $draft_posts_count;