Note: This code snippet applies to WordPress Posts (WordPress Dashboard > Posts > Categories).
Find Your Category ID
Here are the steps for finding your Category IDs to input into the code snippets below.
- Pull up your Categories in the WordPress Dashboard.
- Hover over the name of the Category.
- Look at the bottom of your browser to take note of your ID.
Hide Single Category
Modify Line 3, replacing ‘xxx’ with the ID of your Category.
function wpb_search_filter($query)
{
if ($query->is_search && !is_admin()) {
$query->set("cat", "-xxx");
}
return $query;
}
add_filter("pre_get_posts", "wpb_search_filter");
Hide Multiple Categories
Modify Line 3, replacing ‘xxx’ with the ID of each Category.
function wpb_search_filter($query)
{
if ($query->is_search && !is_admin()) {
$query->set("cat", "-xxx, -xxx, -xxx");
}
return $query;
}
add_filter("pre_get_posts", "wpb_search_filter");