最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Problem ID to exclude specific posts from category

programmeradmin2浏览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.

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.

Share Improve this question edited May 4, 2020 at 18:36 shanebp 5,0857 gold badges27 silver badges40 bronze badges asked May 4, 2020 at 13:59 DaitarnDaitarn 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论