is_post_status_viewable()
云策文档标注
概述
is_post_status_viewable() 函数用于判断文章状态是否被视为“可查看”。它基于文章状态对象的属性(如 public 或 publicly_queryable)和内置状态(如 publish)的 public 值进行评估,并可通过过滤器进行自定义。
关键要点
- 函数接受字符串或 stdClass 对象作为参数,返回布尔值表示文章状态是否可查看。
- 对于内置文章状态(如 publish),使用 public 属性;对于其他状态,使用 publicly_queryable 属性。
- 如果参数是标量但非字符串,或文章状态对象不存在、internal 或 protected,则返回 false。
- 提供 is_post_status_viewable 过滤器,允许开发者自定义判断逻辑,但返回值必须为严格布尔类型以确保兼容性。
代码示例
function is_post_status_viewable( $post_status ) {
if ( is_scalar( $post_status ) ) {
if ( ! is_string( $post_status ) ) {
return false;
}
$post_status = get_post_status_object( $post_status );
if ( ! $post_status ) {
return false;
}
}
if (
! is_object( $post_status ) ||
$post_status->internal ||
$post_status->protected
) {
return false;
}
$is_viewable = $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
/**
* Filters whether a post status is considered "viewable".
*
* The returned filtered value must be a boolean type to ensure
* `is_post_status_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 status is "viewable" (strict type).
* @param stdClass $post_status Post status object.
*/
return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status );
}注意事项
- 过滤器 is_post_status_viewable 的返回值必须为严格布尔类型(true 或 false),非布尔值将导致函数返回 false,以避免 PHP 8.1+ 中的类型错误。
- 函数自 WordPress 5.7.0 引入,5.9.0 版本添加了过滤器钩子。
原文内容
Determines whether a post status is considered “viewable”.
Description
For built-in post statuses such as publish and private, the ‘public’ value will be evaluated.
For all others, the ‘publicly_queryable’ value will be used.
Parameters
$post_statusstring|stdClassrequired-
Post status name or object.
Source
function is_post_status_viewable( $post_status ) {
if ( is_scalar( $post_status ) ) {
if ( ! is_string( $post_status ) ) {
return false;
}
$post_status = get_post_status_object( $post_status );
if ( ! $post_status ) {
return false;
}
}
if (
! is_object( $post_status ) ||
$post_status->internal ||
$post_status->protected
) {
return false;
}
$is_viewable = $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
/**
* Filters whether a post status is considered "viewable".
*
* The returned filtered value must be a boolean type to ensure
* `is_post_status_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 status is "viewable" (strict type).
* @param stdClass $post_status Post status object.
*/
return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status );
}
Hooks
- apply_filters( ‘is_post_status_viewable’, bool $is_viewable, stdClass $post_status )
-
Filters whether a post status is considered “viewable”.