Can query_vars be used to exclude posts in the main query in pre_get_posts?
Question
Is there a way to exclude posts in the main query in pre_get_posts that are already in query_vars?
For example: I have three $query_vars
that work to get posts depending on the category. Is there a way to have the main query exclude posts in the three $query_vars
?
Here’s my pre_get_post
function:
// set up query vars
function ctrl_dly_podcast( $query_vars ){
$query_vars[] = 'ctrl_podcasts_status';
return $query_vars;
}
add_filter( 'query_vars', 'ctrl_dly_podcast' );
function first_video_front( $query_vars ){
$query_vars[] = 'first_vid';
return $query_vars;
}
add_filter( 'query_vars', 'first_video_front' );
function first_post_front( $query_vars ){
$query_vars[] = 'first_post';
return $query_vars;
}
add_filter( 'query_vars', 'first_post_front' );
// pre_get_post 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['first_vid'] )) {
$query->set('tax_query', array(array('taxonomy' => 'category','field' => 'slug','terms' => array( 'video' ),'operator'=> 'IN')));
$query->set('posts_per_page', 1);
}
if( isset( $query->query_vars['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);
}
return $query;
}
add_action( 'pre_get_posts', 'opby_query' );
0
2 months
0 Answers
12 views
0
Leave an answer
You must login or register to add a new answer .