Site logo

Archived topic

How To Hide posts from certain category from search?

13 replies · Started by Daniel on October 4, 2020

Viewing posts 1–14 of 14

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.

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/

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

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

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.

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?

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.

got it, thank you, David.

Glad we could be of help

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.

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.

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)?

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.

This archived topic is closed to new replies.