这在很大程度上取决于您决定在引导/请求序列中的何处尝试检索状态,还取决于某人、某件事、插件或主题是否正在做某些事情,是否时髦,但后者的可能性较小。
有用的API函数:
get_post_statuses docs |
source检索内置post类型的状态postget\\u available\\u post\\u状态get_available_post_statuses docs | source
检索特定帖子类型的状态,但默认为post 如果未提供值get_post_stati docs| source | RECOMMENDED
检索状态,默认情况下将检索所有状态,包括内置状态。此函数环绕global $wp_post_statuses; 因此,建议您使用该函数代替直接调用$wp_post_statuses.如果您拨打全球$wp_post_statuses, 请注意您调用它的位置以及在操作或筛选器中的时间,钩子是在状态注册点之前还是之后激发,或者如果在同一钩子中,您的优先级是在注册之前还是之后。
设想如下:
注册类型:
add_action(\'init\', function () {
register_post_status(\'custom_status\', array(
\'label\' => _x(\'My Status\', \'post\'),
\'public\' => true,
// shortened for brevity
));
}, 20);
检索类型:
add_action(\'init\', function () {
global $wp_post_statuses;
var_dump( $wp_post_statuses );
# result... no \'custom_status\'
/*
array(
"publish",
"future",
"draft",
"pending",
"private",
"trash",
"auto-draft",
"inherit",
)
*/
}, 10);
检索类型:指定等于或高于用于注册状态的优先级的优先级:
add_action(\'init\', function () {
global $wp_post_statuses;
var_dump( $wp_post_statuses );
# result... with \'custom_status\'
/*
array(
"publish",
"future",
"draft",
"pending",
"private",
"trash",
"auto-draft",
"inherit",
"custom_status" // <-- here we go
)
*/
}, PHP_INT_MAX); // <-- high priority
或者,如果您可以使用另一个钩子,即稍后触发的钩子,例如,如果有人正在上注册状态
init 你也可以选择
wp_loaded 运行逻辑,此时将包含所需的状态等。
找到注册状态的位置并从那里开始。如果您使用的是好的IDE或类似的IDE,那么您可以搜索整个代码库,包括核心代码库和供应商代码库(插件、主题),以很容易地找到您想要跟踪的特定状态。例如,PHPStorm有一个Find In Path 工具来帮助您实现这一点。大多数其他工具都有类似的功能。
关于WordPress加载顺序的一些有用资料:
Is there a flowchart for WordPress loading sequence?
How to get WordPress' hooks/actions run sequence?