Using WordPress ‘is_post_type_viewable()’ PHP function

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

Usage

$is_viewable = is_post_type_viewable( $post_type );

Example:
Input:

is_post_type_viewable('post');

Output:

true

Parameters

  • $post_type (string|WP_Post_Type) – Required. Post type name or object.

More information

See WordPress Developer Resources: is_post_type_viewable

Examples

Check if a built-in post type is viewable

This example checks if the built-in ‘post’ post type is viewable.

$is_post_viewable = is_post_type_viewable('post');
echo $is_post_viewable; // Output: true

Check if a custom post type is viewable

This example checks if a custom post type ‘portfolio’ is viewable.

$is_portfolio_viewable = is_post_type_viewable('portfolio');
echo $is_portfolio_viewable; // Output depends on 'publicly_queryable' value

Using ‘is_post_type_viewable()’ in a conditional statement

This example displays a message depending on whether a post type is viewable.

if (is_post_type_viewable('page')) {
    echo 'Page post type is viewable';
} else {
    echo 'Page post type is not viewable';
}

Checking multiple post types

This example checks multiple post types and echoes their viewable status.

$post_types = array('post', 'page', 'portfolio');

foreach ($post_types as $post_type) {
    $is_viewable = is_post_type_viewable($post_type);
    echo ucfirst($post_type) . ' is ' . ($is_viewable ? 'viewable' : 'not viewable') . '.';
}

Hide a custom post type using ‘is_post_type_viewable’ filter

This example hides a custom post type called ‘sample_post_type’ using the ‘is_post_type_viewable’ filter.

add_filter('is_post_type_viewable', 'hide_sample_post_type', 10, 2);

function hide_sample_post_type($is_viewable, $post_type) {
    if (false == $is_viewable || 'sample_post_type' === $post_type->name) {
        return false;
    }
    return true;
}