WordPress函数:WP_Query 根据文章状态查询文章

追格官方小助手/ 2022年07月09日/ WordPress/ 浏览 1291

WordPress中,文章有各种状态,如下:


publish - 已发布

pending - 已审核

draft - 草稿

auto-draft - 自动草稿

future - 定时发布

private - 私有文章

inherit - 继承

trash - 垃圾箱


使用 WP_Query 查询文章时,参数 post_status 默认值为 'publish',即 WP_Query 默认只查询发布状态的文章。


代码示例:


// 单一状态
$query = new WP_Query( array( 'post_status' => 'draft' ) );

// 多个状态
$args = array(
    'post_status' => array( 'pending', 'draft', 'future' )
);
$query = new WP_Query( $args );


另 post_status 支持 'any',可以查询出除了 'inherit', 'trash' 和 'auto-draft' 三种状态以外的所有文章。


$args = array(
    'post_status' => 'any',
    'post_type'   => 'attachment'
);
$query = new WP_Query( $args );


WP_Query 的基础用法可以参考文章:WP_Query 的基础用法简介

发表评论

暂无评论,抢个沙发...

客服 工单