0

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:

// 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
  • excluding things this way is extremely slow and heavy on the database. It's much faster to ask for posts in eery single category ( without mentioning category X ), then it is to tell the database you don't want categry X. Exclusions involve the creation of temporary database tables and full table copies and a full table scan, the scaling and performance cost of such a query can be massive.
    – Tom J Nowell
    Nov 25 '20 at 12:57
  • Also, don't double post questions to get more traffic, it's spammy and will get you shadow banned by the automated anti-spam systems. ( it can see your deleted questions, as can moderators )
    – Tom J Nowell
    Nov 25 '20 at 12:59
1

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;
}
1
  • Sorry but that didn't work. I've updated the code above to show a better working example. Nov 26 '20 at 10:28
0

Did you try with this one?

$query->set( 'post__not_in', array( 1, 1 ) );

In the array( 1, 1 ) is array( $PostID, $PostID ) which post ids you want to exclude from main query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.