我正在使用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\' );