Site logo

[Resolved] hide a specific category

Home Forums Support [Resolved] hide a specific category

Home Forums Support hide a specific category

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #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!
    DM

    #2237329
    Fernando
    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 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!

    #2239692
    DM

    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’);

    #2239717
    Fernando
    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 123 with your category ID.

    Sample of how to get the ID: https://share.getcloudapp.com/geuRZrbY

    Hope this helps!

    #2239786
    DM

    Thanks! It worked! 🙂

    #2239789
    Fernando
    Customer Support

    You’re welcome DM! Glad that worked! 🙂

    #2247600
    DM

    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

    #2247776
    Fernando
    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!

    #2247784
    DM

    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

    #2247797
    Fernando
    Customer Support

    I see. I replied to you in that thread.

    Kindly let us know how it goes.

    #2247808
    DM

    Thanks! all set! 🙂

    #2247812
    Fernando
    Customer Support

    You’re welcome! Glad it worked! 🙂

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.