is_post_type_viewable

函数
is_post_type_viewable ( $post_type )
参数
  • (string|WP_Post_Type) $post_type Post type name or object.
    Required:
返回值
  • (bool) Whether the post type should be considered viewable.
定义位置
相关方法
is_post_status_viewableis_post_publicly_viewableis_post_type_hierarchicalis_post_type_archiveis_taxonomy_viewable
引入
4.4.0
弃用
-

is_post_type_viewable – 这个函数检查一个给定的文章类型是否可以公开查看。如果文章类型是可公开查看的,则返回true,否则返回false。

确定一个文章类型是否被认为是”viewable”。

对于内置的文章类型,如文章和页面,将评估”public”值。对于所有其他类型,将使用’public_queryable’值。

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 );
}

常见问题

FAQs
查看更多 >