- This topic has 11 replies, 2 voices, and was last updated 3 years, 10 months ago by
Fernando.
-
AuthorPosts
-
May 29, 2022 at 3:31 pm #2237271
DM
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!
DMMay 29, 2022 at 6:11 pm #2237329Fernando Customer Support
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
uncategorizedwith 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
uncategorizedwith your specific category here as well.Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets
Hope this helps!
May 31, 2022 at 3:54 pm #2239692DM
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’);May 31, 2022 at 5:18 pm #2239717Fernando Customer Support
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
123with your category ID.Sample of how to get the ID: https://share.getcloudapp.com/geuRZrbY
Hope this helps!
May 31, 2022 at 7:38 pm #2239786DM
Thanks! It worked! 🙂
May 31, 2022 at 7:46 pm #2239789Fernando Customer Support
You’re welcome DM! Glad that worked! 🙂
June 8, 2022 at 11:15 am #2247600DM
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!
DMJune 8, 2022 at 5:29 pm #2247776Fernando Customer Support
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!
June 8, 2022 at 5:55 pm #2247784DM
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
June 8, 2022 at 6:24 pm #2247797Fernando Customer Support
I see. I replied to you in that thread.
Kindly let us know how it goes.
June 8, 2022 at 6:52 pm #2247808DM
Thanks! all set! 🙂
June 8, 2022 at 6:54 pm #2247812Fernando Customer Support
You’re welcome! Glad it worked! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.