Using WordPress ‘is_post_status_viewable()’ PHP function

The is_post_status_viewable() WordPress PHP function determines whether a post status is considered “viewable”.

Usage

$is_viewable = is_post_status_viewable( $post_status );

Example:

Input:

$post_status = 'publish';

Output:

true

Parameters

  • $post_status (string|stdClass) – Required: Post status name or object.

More information

See WordPress Developer Resources: is_post_status_viewable()

Examples

Check if a post with ‘publish’ status is viewable

This example checks if a post with ‘publish’ status is viewable:

$post_status = 'publish';
$is_viewable = is_post_status_viewable($post_status);

if ($is_viewable) {
    echo "The post is viewable!";
} else {
    echo "The post is not viewable.";
}

Check if a post with ‘private’ status is viewable

This example checks if a post with ‘private’ status is viewable:

$post_status = 'private';
$is_viewable = is_post_status_viewable($post_status);

if ($is_viewable) {
    echo "The post is viewable!";
} else {
    echo "The post is not viewable.";
}

Check if a custom post status is viewable

This example checks if a custom post status called ‘archived’ is viewable:

$post_status = 'archived';
$is_viewable = is_post_status_viewable($post_status);

if ($is_viewable) {
    echo "The post is viewable!";
} else {
    echo "The post is not viewable.";
}

Check if a post status is viewable using a post object

This example retrieves a post object and checks if its status is viewable:

$post_id = 1; // Replace with a valid post ID
$post = get_post($post_id);
$is_viewable = is_post_status_viewable($post->post_status);

if ($is_viewable) {
    echo "The post is viewable!";
} else {
    echo "The post is not viewable.";
}

Check if a post status is viewable for multiple posts

This example checks if the post status is viewable for multiple posts using a loop:

$post_ids = array(1, 2, 3); // Replace with valid post IDs

foreach ($post_ids as $post_id) {
    $post = get_post($post_id);
    $is_viewable = is_post_status_viewable($post->post_status);

    if ($is_viewable) {
        echo "Post ID {$post_id} is viewable!";
    } else {
        echo "Post ID {$post_id} is not viewable.";
    }
}