get_post_status_object()
云策文档标注
概述
get_post_status_object() 函数用于通过名称检索已注册的文章状态对象。它检查全局变量 $wp_post_statuses 中是否存在指定状态,并返回对应的对象或 null。
关键要点
- 函数接受一个字符串参数 $post_status,表示注册的文章状态名称。
- 返回值为 stdClass 对象或 null,如果状态未注册或参数无效则返回 null。
- 内部实现基于全局数组 $wp_post_statuses,可直接访问但推荐使用此函数以确保安全。
- 与 register_post_status() 相关,用于管理自定义文章状态。
代码示例
$obj = get_post_status_object( 'publish' );
echo esc_html( $obj->label );global $wp_post_statuses;
$obj = $wp_post_statuses['publish'];
echo esc_html( $obj->label );注意事项
- 函数自 WordPress 3.0.0 版本引入,兼容性良好。
- 在 REST API、权限检查、查询处理等多个核心功能中被广泛使用。
- 输出对象包含 label、public、name 等属性,可用于动态显示状态信息。
原文内容
Retrieves a post status object by name.
Description
See also
Parameters
$post_statusstringrequired-
The name of a registered post status.
Source
function get_post_status_object( $post_status ) {
global $wp_post_statuses;
if ( ! is_string( $post_status ) || empty( $wp_post_statuses[ $post_status ] ) ) {
return null;
}
return $wp_post_statuses[ $post_status ];
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 2 content
Pixelbart
Examples
$obj = get_post_status_object( 'publish' ); echo esc_html( $obj->label );Alternatively, it also works like this:
global $wp_post_statuses; $obj = $wp_post_statuses['publish']; echo esc_html( $obj->label );print_r( $obj ) looks like this:
stdClass Object ( [label] => Published [label_count] => Array ( [0] => Published (%s) [1] => Published (%s) [singular] => Published (%s) [plural] => Published (%s) [context] => [domain] => ) [exclude_from_search] => [_builtin] => 1 [public] => 1 [internal] => [protected] => [private] => [publicly_queryable] => 1 [show_in_admin_status_list] => 1 [show_in_admin_all_list] => 1 [date_floating] => [name] => publish )