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.
- WordPress Dashboard > Posts > Categories
- Hover over the name of the category
- Look at the bottom of your browser to take note of your ID (e.g., tag_ID=123)
Hide a Single Category
Modify line 3, replacing ‘xxx’ with your category ID.
PHP
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.
PHP
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");