get_post_status()
云策文档标注
概述
get_post_status() 函数用于根据文章 ID 或文章对象检索文章状态。它处理附件类型的特殊逻辑,并返回状态字符串或 false。
关键要点
- 函数接受可选参数 $post,可以是整数 ID 或 WP_Post 对象,默认为全局 $post 或 null。
- 返回值为字符串(成功时)或 false(失败时),常见状态包括 publish、draft、private、trash、auto-draft、inherit 等。
- 对于附件类型,如果状态为 'inherit',则返回父文章状态;未附加的附件默认为 'publish'。
- 函数包含 apply_filters('get_post_status', $post_status, $post) 钩子,允许过滤状态。
- 注意:在循环中使用时,建议显式传递 ID 或文章对象以确保正确性。
代码示例
// 基本用法:获取当前文章状态
$status = get_post_status( get_the_ID() );
// 获取本地化状态标签
echo get_post_status_object( get_post_status() )->label;注意事项
- 插件可能引入自定义状态类型,函数也可能返回这些状态。
- 参数默认行为可能因上下文而异,在循环中最好显式指定参数。
原文内容
Retrieves the post status based on the post ID.
Description
If the post ID is of an attachment, then the parent post status will be given instead.
Parameters
$postint|WP_Postoptional-
Post ID or post object. Defaults to global $post.
Default:
null
Source
function get_post_status( $post = null ) {
// Normalize the post object if necessary, skip normalization if called from get_sample_permalink().
if ( ! $post instanceof WP_Post || ! isset( $post->filter ) || 'sample' !== $post->filter ) {
$post = get_post( $post );
}
if ( ! is_object( $post ) ) {
return false;
}
$post_status = $post->post_status;
if (
'attachment' === $post->post_type &&
'inherit' === $post_status
) {
if (
0 === $post->post_parent ||
! get_post( $post->post_parent ) ||
$post->ID === $post->post_parent
) {
// Unattached attachments with inherit status are assumed to be published.
$post_status = 'publish';
} elseif ( 'trash' === get_post_status( $post->post_parent ) ) {
// Get parent status prior to trashing.
$post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
if ( ! $post_status ) {
// Assume publish as above.
$post_status = 'publish';
}
} else {
$post_status = get_post_status( $post->post_parent );
}
} elseif (
'attachment' === $post->post_type &&
! in_array( $post_status, array( 'private', 'trash', 'auto-draft' ), true )
) {
/*
* Ensure uninherited attachments have a permitted status either 'private', 'trash', 'auto-draft'.
* This is to match the logic in wp_insert_post().
*
* Note: 'inherit' is excluded from this check as it is resolved to the parent post's
* status in the logic block above.
*/
$post_status = 'publish';
}
/**
* Filters the post status.
*
* @since 4.4.0
* @since 5.7.0 The attachment post type is now passed through this filter.
*
* @param string $post_status The post status.
* @param WP_Post $post The post object.
*/
return apply_filters( 'get_post_status', $post_status, $post );
}
Hooks
- apply_filters( ‘get_post_status’, string $post_status, WP_Post $post )
-
Filters the post status.
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |
Skip to note 6 content
Aurovrata Venet
will return one of: publish, future, draft, pending, private
Skip to note 7 content
Codex
A basic example:
Skip to note 8 content
twinpictures
To return the localized label for the post status, get_post_status_object must be used:
echo get_post_status_object( get_post_status( ) )->label;Skip to note 9 content
internetfootprint
Basic example:
Default post status includes:
publish,future,draft,pending,private.'publish' === $current_status == 'publish'should be..'publish' === $current_statusSkip to note 10 content
Peter Hardy-vanDoorn
Documentation above says both that, if no ID or $post is passed then it will default to both the global $post AND null.
In my testing it does NOT default to the global $post, so if used in the Loop you must still pass either the ID or the $post, eg:
get_post_status ( get_the_ID() )get_post_status()returns the global$poststatus if no parameter is provided.