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 badge1 Answer
Reset to default 0Try 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.