个性化阅读
专注于IT技术分析

从查询中排除预定的post

我想从我的自定义主题中排除预定的帖子。我用了动作钩

pre_get_posts

在我的函数文件中仅检索已发布的帖子, 但计划的帖子仍然出现:

function exclude_scheduled_posts( $query ){

if( $query->is_home() && $query->is_main_query() ){
    $query->set('post_status' , array('publish'));
}
return ;
}
add_action('pre_get_posts', 'exclude_scheduled_posts');

如何过滤预定的帖子?


#1


你可以在代码中使用post_status参数过滤预定的帖子。.你可以在函数代码中遵循此操作。

* 'publish' - a published post or page
* 'pending' - post is pending review
* 'draft' - a post in draft status
* 'auto-draft' - a newly created post, with no content
* 'future' - a post to publish in the future
* 'private' - not visible to users who are not logged in
* 'inherit' - a revision. see get_children.
* 'trash' - post is in trash.

// WP_Query arguments
$args = array(
    'post_type'              => array( 'post' ), 'post_status'            => array('publish', 'pending', 'draft', 'auto-draft', 'private', 'inherit', 'trash'), );

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
    $query->the_post();
    // do something
    }
} else {
    // no posts found
}

wp_reset_postdata();
赞(0)
未经允许不得转载:srcmini » 从查询中排除预定的post

评论 抢沙发

评论前必须登录!