Archived topic
hide a specific category
11 replies · Started by DM on May 29, 2022
Hi there!
I'd like to remove a specific category from showing up in search results from the widgets at the bottom of my page. I'd also like to turn off the category's archive page.
Thanks!
DM
Hi DM,
To exclude a category for the Search functionality, here is a PHP snippet you may try:
function exclude_category_from_search($query) {
if ($query->is_search) {
$cat_id = get_cat_ID('uncategorized');
$query->set('cat', '-'.$cat_id);
}
return $query;
}
add_filter('pre_get_posts','exclude_category_from_search');
Kindly replace uncategorized with your specific category.
Reference: https://www.webhostinghero.com/how-to-exclude-categories-from-search-in-wordpress/
To disable a specific category archive page, here is another PHP snippet you may try:
add_action('template_redirect', 'my_remove_archive');
function my_remove_archive(){
if( is_category('uncategorized') ) {
global $wp_query;
$wp_query->set_404(); //set to 404 not found page
}
}
Kindly replace uncategorized with your specific category here as well.
Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets
Hope this helps!
Thanks for your help! That did NOT work for removing the category "roundup" from my search; the category is still showing up in the "Explore A Category" widget at the bottom of my page. It DID work for removing the archive page.
I also want it to stop showing up in my page feed, but so far I cannot get it to filter out at mydomain/feed/
function exclude_category_from_search($query) {
if ($query->is_search) {
$cat_id = get_cat_ID('roundup');
$query->set('cat', '-'.$cat_id);
}
return $query;
}
add_filter('pre_get_posts','exclude_category_from_search');
add_action('template_redirect', 'my_remove_archive');
function my_remove_archive(){
if( is_category('roundup') ) {
global $wp_query;
$wp_query->set_404(); //set to 404 not found page
}
}
function wpsites_exclude_category_feed($query) {
if ($query->is_feed) {
$query->set('cat', '-'.$cat_id);
}
return $query;
}
add_filter('pre_get_posts','wpsites_exclude_category_feed');
I see. You’re using a Category Block which has no filter to exclude a category.
Alternatively, you can try adding this CSS in Appearance > Customize > Additional CSS:
.wp-block-categories-dropdown option[value="1823"] {
display:none;
}
To remove the category from the feed, try this PHP:
function wpsites_exclude_category_feed($query) {
if ($query->is_feed) {
$query->set('cat','-123');
}
return $query;
}
add_filter('pre_get_posts','wpsites_exclude_category_feed');
Kindly replace 123 with your category ID.
Sample of how to get the ID: https://share.getcloudapp.com/geuRZrbY
Hope this helps!
Thanks! It worked! :)
You’re welcome DM! Glad that worked! :)
Hi there!
The category I'd like to suppress is coming up in the Next/Last post area. Can you tell me how to suppress specific categories here?
Thanks!
DM
To have a better understanding of the issue, may we know the specific steps to replicate/locate the issue from the link you provided?
Kindly let us know. Hope to hear from you soon!
I found this answer from Tom and the last code works but removes the forward "<" and ">" back arrows. https://generatepress.com/forums/topic/post-navigation-how-exclude-a-category-from-next-and-previous-post-links/page/2/
Here's his link to the code: https://gist.github.com/generatepress/803a950d6381dd98e6bc4418b817ed82
I see. I replied to you in that thread.
Kindly let us know how it goes.
Thanks! all set! :)
You're welcome! Glad it worked! :)