0

I'm trying to exclude specific posts from category/archive with these functions:

function exclude_single_posts_cat($query) {
      if ( is_admin() || ! $query->is_main_query() )
        return;
     if ($query->is_archive() ) {
          $query->set('post__not_in', array('1','2','3'));}} 
          //I've also tried** ('-1','-2','-3') or without quotes
      }
}
add_action('pre_get_posts', 'exclude_single_posts_cat',);

Or:

function exclude_single_posts_cat($query) {
     if ($query->is_category() AND $query->is_main_query()) {
        $query->set('post__not_in', array('1','2','3'));}} 
       //I've also tried** ('-1','-2','-3') or without quotes.
     }
}
add_action('pre_get_posts', 'exclude_single_posts_cat');

But something happens that I can't explain, that is, there disappear posts that don't correspond to the specified ID (following the example, the hidden posts may be '4', '5', '6' ). Could someone tell me why it happens and where I'm wrong, please?

Thank you in advance.

1 Answer 1

0

Try this:

function exclude_single_posts_cat($query) {
    if ( is_admin() || ! $query->is_main_query() )
        return;
    if ( $query->is_archive() ) {
        $query->set('post__not_in', array('1','2','3'));
    }
}
add_action('pre_get_posts', 'exclude_single_posts_cat');

Issue: The issue in your first approach is you have added extra curly brackets and comma.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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