Hide Post Categories From WordPress Search

If you care about website performance and easier administration, I recommend using a code snippet plugin like WPCodeBox (or similar) for adding code to your website.

Instructions

  1. Create a new PHP code snippet.
  2. Copy the contents of code snippet below.
  3. Paste the contents into your code snippet.
  4. Review any notes that I’ve provided.
  5. Save and enable the code snippet.
  6. Test.

Snippet

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. Pull up your Categories in the WordPress Dashboard.
  2. Hover over the name of the Category.
  3. 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");

Video Tutorial

🔥NEW!🔥
Property Starter Site
MyListing Add-on
Launch Sale Ends: June 24th.

Club Guarantee

If you notice an issue with this product, I’ll fix it for free and get you an updated version.

Club Guarantee

If you notice an issue with a Club Product and the problem exists on the corresponding Starter Site, I’ll fix it for free.