Excluded a Category from Archive Layout

Hi,

I just went in to fix my archive layout and found out not to use a Looper Provider any more. That fixed the issue I was having where when you clicked on a term from the list it wouldn’t display just that category. HOWEVER, now I can’t exclude a category from my posts list. I’ve tried using a conditional, but the category is still displaying.

Thanks for any help you can give.
Jen

Hello @petitshoo,

Thanks for writing in! If you want to exclude a particular category in your custom post type archive, you will have to use custom PHP coding for that because you will need to modify the WordPress Loop to exclude those items that you do not want it to display.

Be advised that custom coding is beyond the scope of our support under our Support Policy. If you are unfamiliar with code and resolving potential conflicts, you may select our One service for further assistance.

Check out this old thread as your reference:

Thanks for letting me know. I would like to make this a feature request since it is easy to do when you are creating your own looper provider. It would make sense to be able to exclude a category on an archive page.

Thanks,
Jen

Also, for folks who may need it, here is the PHP I used to exclude a category from the custom post type’s archive page (replace ‘resources’ in all places with your custom post type)

//Exclude Category from Resources
function exclude_terms_from_resources($query) {
if (!is_admin() && $query->is_main_query()) {
if (isset($query->query_vars[‘post_type’]) && $query->query_vars[‘post_type’] === ‘resources’) {
$query->set(‘tax_query’, array(
array(
‘taxonomy’ => ‘toolkit-category’, // Use your custom taxonomy slug here
‘field’ => ‘term_id’,
‘terms’ => array(788), // Replace 45 with the ID of the term to exclude
‘operator’ => ‘NOT IN’,
),
));
}
}
}
add_action(‘pre_get_posts’, ‘exclude_terms_from_resources’);

Thanks for sharing @petitshoo.