[Support request] How To Hide posts from certain category from search?

Home Forums Support [Support request] How To Hide posts from certain category from search?

Home Forums Support How To Hide posts from certain category from search?

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1472109
    Daniel

    Hi,

    I tried to find an answer on the forum about this but couldn’t. Can you please tell me how could I Hide posts from a certain category (ex. uncategorized) from the search option on my website? I want to completely hide them and make them inaccessible.

    #1472125
    Elvin
    Staff
    Customer Support

    Hi Daniel,

    You can definitely do this w/ third party plugins or a PHP snippet.

    Example PHP snippet:

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

    This will hide all posts under the “uncategorized” category from the search query.

    Reference: https://www.webhostinghero.com/how-to-exclude-categories-from-search-in-wordpress/

    #1472130
    Daniel

    Perfect, thank you, Elvin. Perhaps a silly question, but in which theme file in the Theme Editor do I add the code?

    #1472138
    Elvin
    Staff
    Customer Support

    You can check this article on how to add PHP snippet codes.

    https://docs.generatepress.com/article/adding-php/

    #1472829
    Daniel

    Thank you for sharing this, Elivn; I’ll look into this option. Is there a way to do this without using a plugin?

    #1472832
    Elvin
    Staff
    Customer Support

    Thank you for sharing this, Elivn; I’ll look into this option. Is there a way to do this without using a plugin?

    You can definitely do without one. You can either add the PHP snippet on your child theme’s functions.php or the parent theme’s functions.php.

    #1472841
    Daniel

    Just to make sure, is that the file named Theme Functions (functions.php)? And if so, is there a particular part of the file where I insert the link, or do I just add it to the bottom of the file?

    #1472923
    David
    Staff
    Customer Support

    It must go in you Child themes functions.php – not the parent theme file as this will be lost when the theme is updated.

    Yes, it can be added to the end of the functions file.

    #1472938
    Daniel

    got it, thank you, David.

    #1474090
    David
    Staff
    Customer Support

    Glad we could be of help

    #1557319
    Daniel

    Hi guys, following up on this thread again as I thought it would be better than opening a new one.

    The solution was working for a couple of months but suddenly, uncategorized posts became searchable again (noticed this a few days ago)

    I recently moved to a new SEO plugin and I can’t think of any other changes that could affect this but now, I can’t find the child Child themes functions.php anywhere.

    #1557345
    Elvin
    Staff
    Customer Support

    That’s strange.

    SEO plugins normally don’t do anything to theme files.

    Can you check on your Appearance > Themes if the child theme is still the active theme?

    Let us know.

    #1564746
    Daniel

    Hi Elvin,

    I checked and the theme file was there. I removed it and install it again but it still doesn’t appear in the editor. As I mentioned, I don’t remember making any changes to the theme or the website. Do you think it might be related to the theme update (if I recall correctly, there was one a couple of weeks ago)?

    #1565007
    Elvin
    Staff
    Customer Support

    Updating generatepress shouldn’t do anything to any child theme folder as theme updates only do changes within its .../themes/generatepress files. It’s basically just overwriting older versions of the file with the newer ones or simply add ones that doesn’t exist on the previous versions.

    You can try checking your child theme’s folder for any permission change as these can also have an effect on how your child theme files are being read.

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