- This topic has 13 replies, 3 voices, and was last updated 2 years, 5 months ago by
Elvin.
-
AuthorPosts
-
October 4, 2020 at 2:12 pm #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.
October 4, 2020 at 2:30 pm #1472125Elvin
StaffCustomer SupportHi 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/
October 4, 2020 at 2:41 pm #1472130Daniel
Perfect, thank you, Elvin. Perhaps a silly question, but in which theme file in the Theme Editor do I add the code?
October 4, 2020 at 2:44 pm #1472138Elvin
StaffCustomer SupportYou can check this article on how to add PHP snippet codes.
October 5, 2020 at 4:45 am #1472829Daniel
Thank you for sharing this, Elivn; I’ll look into this option. Is there a way to do this without using a plugin?
October 5, 2020 at 4:48 am #1472832Elvin
StaffCustomer SupportThank 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.
October 5, 2020 at 5:02 am #1472841Daniel
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?
October 5, 2020 at 5:56 am #1472923David
StaffCustomer SupportIt 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.
October 5, 2020 at 6:07 am #1472938Daniel
got it, thank you, David.
October 6, 2020 at 12:25 am #1474090David
StaffCustomer SupportGlad we could be of help
November 29, 2020 at 3:45 pm #1557319Daniel
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.
November 29, 2020 at 3:59 pm #1557345Elvin
StaffCustomer SupportThat’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.
December 2, 2020 at 4:25 pm #1564746Daniel
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)?
December 2, 2020 at 10:36 pm #1565007Elvin
StaffCustomer SupportUpdating 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.
-
AuthorPosts
- You must be logged in to reply to this topic.