钩子文档

get_post_status

💡 云策文档标注

概述

get_post_status 是一个 WordPress 过滤器,用于修改或过滤文章状态。它允许开发者在获取文章状态时进行自定义处理,例如基于特定条件更改状态。

关键要点

  • 过滤器名称:get_post_status
  • 参数:$post_status(字符串,文章状态)和 $post(WP_Post 对象,文章对象)
  • 用途:在 get_post_status() 函数中应用,用于动态调整文章状态
  • 版本历史:从 WordPress 4.4.0 引入,5.7.0 版本扩展支持附件文章类型

代码示例

function wpdocs_change_post_status( $post_status, $post ) {
    // 为特定文章 ID 更改状态
    if ( 1 === $post->ID ) {
        $post_status = 'publish';
    }

    return $post_status;
}
add_filter( 'get_post_status', 'wpdocs_change_post_status', 10, 2 );

📄 原文内容

Filters the post status.

Parameters

$post_statusstring
The post status.
$postWP_Post
The post object.

Source

return apply_filters( 'get_post_status', $post_status, $post );

Changelog

Version Description
5.7.0 The attachment post type is now passed through this filter.
4.4.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example: Changed post status of post id 1

    function wpdocs_change_post_status( $post_status, $post ) {
    	// Change post status for particular post
    	if ( 1 === $post->ID ) {
    		$post_status = 'publish';
    	}
    
    	return $post_status;
    }
    add_filter( 'get_post_status', 'wpdocs_change_post_status', 10, 2 );