How to ignore posts in pre_get_posts?

Question

I’m using pre_get_posts and looking for a way to exclude posts in the main query. I’m using query_vars to query posts from a specific category and looking for a solution to exclude those posts in the main query.

My code:

// set query vars
function ctrl_dly_podcast( $query_vars ){
   $query_vars[] = 'ctrl_podcasts_status';
   return $query_vars;
}
add_filter( 'query_vars', 'ctrl_dly_podcast' );
function opby_query( $query ) {

// main query
if ( $query->is_home() && $query->is_main_query() ) {
    $query->set('posts_per_page', 15);
};

// second query to get 1 post in a category. This query to be checked against the main query and 
exclude post in the main 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);
}
return $query;
}
add_action( 'pre_get_posts', 'opby_query' );
0
Gregory Schultz 2 months 0 Answers 10 views 0

Leave an answer