Using WordPress ‘is_post_type_viewable’ PHP filter

The is_post_type_viewable WordPress PHP Filter determines if a post type is considered “viewable”.

Usage

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

function your_function_name($is_viewable, $post_type) {
    // your custom code here
    return $is_viewable;
}

Parameters

  • $is_viewable (bool): Whether the post type is “viewable” (strict type).
  • $post_type (WP_Post_Type): Post type object.

More Information

See WordPress Developer Resources: is_post_type_viewable

Examples

Make a custom post type viewable

In this example, we’ll make a custom post type called “special_post” viewable.

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

function make_special_post_viewable($is_viewable, $post_type) {
    if ($post_type->name === 'special_post') {
        return true;
    }
    return $is_viewable;
}

Hide a specific post type

In this example, we’ll hide a post type called “hidden_post” from being viewable.

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

function hide_hidden_post($is_viewable, $post_type) {
    if ($post_type->name === 'hidden_post') {
        return false;
    }
    return $is_viewable;
}

Make all post types viewable

In this example, we’ll make all post types viewable.

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

function make_all_post_types_viewable($is_viewable, $post_type) {
    return true;
}

Hide all post types

In this example, we’ll hide all post types from being viewable.

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

function hide_all_post_types($is_viewable, $post_type) {
    return false;
}

Make post types viewable based on a condition

In this example, we’ll make post types viewable only if the current user is an administrator.

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

function conditionally_make_post_types_viewable($is_viewable, $post_type) {
    if (current_user_can('administrator')) {
        return true;
    }
    return $is_viewable;
}