is_post_type_viewable()WP 4.4.0

Determines whether the specified post type is public (accessible for viewing on the front end of the site). You need to specify an object of the post type.

It checks the publicly_queryable parameter for custom post types and the public parameter for built-in post types, such as post and page.

Uses the global variable $post_type_object.

1 time — 0.000015 sec (very fast) | 50000 times — 0.04 sec (speed of light) | PHP 7.0.5, WP 4.4.2
Hooks from the function

Returns

true|false. true - if the post type is viewable and false otherwise.

Usage

is_post_type_viewable( $post_type );
$post_type(string/object) (required)
The post type object. Since version 4.5, you can specify the post type name. The post type name.

Examples

0

#1 Check if the post type is viewable

Suppose there is a post type myposts and we need to check whether ordinary visitors can see this post type.

$obj = get_post_type_object( 'myposts' );

if( is_post_type_viewable( $obj ) ){
	echo 'viewable post type';
}
else {
	echo 'internal post type';
}

Changelog

Since 4.4.0 Introduced.
Since 4.5.0 Added the ability to pass a post type name in addition to object.
Since 4.6.0 Converted the $post_type parameter to accept a WP_Post_Type object.
Since 5.9.0 Added is_post_type_viewable to filter the result.

is_post_type_viewable() code WP 6.9.1

function is_post_type_viewable( $post_type ) {
	if ( is_scalar( $post_type ) ) {
		$post_type = get_post_type_object( $post_type );

		if ( ! $post_type ) {
			return false;
		}
	}

	if ( ! is_object( $post_type ) ) {
		return false;
	}

	$is_viewable = $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );

	/**
	 * Filters whether a post type is considered "viewable".
	 *
	 * The returned filtered value must be a boolean type to ensure
	 * `is_post_type_viewable()` only returns a boolean. This strictness
	 * is by design to maintain backwards-compatibility and guard against
	 * potential type errors in PHP 8.1+. Non-boolean values (even falsey
	 * and truthy values) will result in the function returning false.
	 *
	 * @since 5.9.0
	 *
	 * @param bool         $is_viewable Whether the post type is "viewable" (strict type).
	 * @param WP_Post_Type $post_type   Post type object.
	 */
	return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type );
}