Join

Hide Post Categories From WordPress Search

  • Guides reflect how I’m currently using the solution for myself and my clients. 
  • If I don’t cover a specific feature, it implies I’m not using it.
  • If I don’t cover a specific setting, it implies the defaults are acceptable or the options are obvious.

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.

  1. WordPress Dashboard > Posts > Categories
  2. Hover over the name of the category
  3. 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");

Video Tutorial