如何忽略Pre_Get_Posts中的帖子?

时间:2020-11-25 作者:Gregory Schultz

我正在使用pre_get_posts 并寻找在主查询中排除帖子的方法。我正在使用query_vars 从特定类别查询帖子,并寻找在主查询中排除这些帖子的解决方案。

我的代码:

// function to setup query_vars
function ctrl_dly_podcast( $query_vars ){
  $query_vars[] = \'ctrl_podcasts_status\';
  return $query_vars;
}
add_filter( \'query_vars\', \'ctrl_dly_podcast\' );
function index_first_post( $query_vars ){
  $query_vars[] = \'index_first_post\';
  return $query_vars;
}
add_filter( \'query_vars\', \'index_first_post\' );
function first_video( $query_vars ){
  $query_vars[] = \'1st_video\';
  return $query_vars;
}
add_filter( \'query_vars\', \'first_video\' );
function rest_posts( $query_vars ){
  $query_vars[] = \'posts_the_rest\';
  return $query_vars;
}
add_filter( \'query_vars\', \'rest_posts\' );


//the pre_get_posts function
function opby_query( $query ) {
if( isset( $query->query_vars[\'ctrl_podcasts_status\'] )) {
    $query->set(\'tax_query\', array(array(\'taxonomy\' => \'category\',\'field\' => \'slug\',\'terms\' => array( \'podcast-control-daily\' ),\'operator\'=> \'IN\')));
    $query->set(\'posts_per_page\', 1);
}
if( isset( $query->query_vars[\'index_first_post\'] )) {
    $query->set(\'tax_query\', array(array(\'taxonomy\' => \'category\',\'field\' => \'slug\',\'terms\' => array( \'podcast-control-daily\' ),\'operator\'=> \'NOT IN\'),array(\'taxonomy\' => \'post_format\',\'field\' => \'slug\',\'terms\' => array( \'video\' ),\'operator\'=> \'NOT IN\')));
    $query->set(\'posts_per_page\', 1);
}
if( isset( $query->query_vars[\'1st_video\'] )) {
    $query->set(\'tax_query\', array(array(\'taxonomy\' => \'category\',\'field\' => \'slug\',\'terms\' => array( \'video\' ),\'operator\'=> \'IN\')));
    $query->set(\'posts_per_page\', 1);
};
// the part I\'m having problems with
if( isset( $query->query_vars[\'posts_the_rest\'] )) {
    $query->set(\'offset\', array($query->query_vars[\'1st_video\'],$query->query_vars[\'index_first_post\'],$query->query_vars[\'ctrl_podcasts_status\']);
    $query->set(\'posts_per_page\', 15);
}
return $query;
}
add_action( \'pre_get_posts\', \'opby_query\' );

2 个回复
SO网友:Ivan Shatsky

What about this one?

function opby_query( $query ) {

    if ( $query->is_main_query() ) {
        if ( $query->is_home() ) {
            $query->set(\'posts_per_page\', 15);
        };
        if ( isset( $query->query_vars[\'ctrl_podcasts_status\'] ) ) {
            // get main query args
            $subquery_args = $query->query_vars;
            // add tax_query filter
            $subquery_args[\'tax_query\'] = array( array(
                \'taxonomy\' => \'category\',
                \'field\' => \'slug\',
                \'terms\' => array( \'podcast-control-daily\' ),
                \'operator\'=> \'IN\'
            ) );
            // get only the first post
            $subquery_args[\'posts_per_page\'] = 1;
            // we need only post IDs
            $subquery_args[\'fields\'] = \'ids\';
            // run the subquery
            $exclude_posts = get_posts( $subquery_args );
            if ( is_array( $exclude_posts ) ) {
                $query->set( \'post__not_in\', $exclude_posts );
            }
        }
    }

    return $query;
}
SO网友:Hims V

你试过这个吗?

$query->set( \'post__not_in\', array( 1, 1 ) );
在数组(1,1)中是要从主查询中排除的post ID的数组($PostID,$PostID)。

相关推荐

ACF:如何在使用wp_Query的同时发布循环中ACF字段的值?

我正在使用ACF向自定义帖子类型添加字段。我创建了一个WP\\u Query实例来设置循环。在循环中,我可以使用\\u title()和\\u content()发布标准字段。但是,ACF函数the\\u字段不会发布附加字段的值。“the\\u field()”和“echo the\\u field()”都不会生成字段值。所以如何准确发布这些值?这是我的循环:$jobs_query = new WP_Query($args) ; if ($jobs_query->have