• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How To Prevent Certain Categories From Showing In WordPress Search

Bhagwad Park

Published on: September 27, 2018

Categories: WordPress 4

A typical WordPress website contains many different types of content. We have the most basic distinction between pages and posts, which serve completely different purposes. We also have different types of blog posts, and some that are meant specifically to be found by our customers, and others for different reasons – like say legal notices.

Not All Content is Targeted Towards Customers

It’s easy to separate the different types of content into categories. We can have one category for informational blog posts, another about personal stories, and so on and so forth. In a well-planned, laser focused website, everything will be relevant to customers in one way or another. But some sites grow organically from different origins.

Perhaps there are certain articles that you don’t want people to see in your site’s search results. You don’t want to remove these posts, because they perhaps still generate traffic, or have inbound links pointing to them, and it doesn’t make sense to lose the link juice.

What I’ll show you in this tutorial, is how to exclude certain categories from showing up in the search results of your website. This way, you can still have the content, but minimize its exposure.

Preventing ONE Category from Appearing in Search

To start with, I’ll assume that you want to exclude just one category from search. It’s an easier method. In the second section, I’ll give you the code to exclude multiple categories.

Let’s say I perform a search on my site, and get the following result – a post with the category “Personal” as shown here:

Personal Category in Search

We’re going to write code to exclude “Personal” posts from the search results.

Getting the Category Slug

Every category has a “slug” and an “ID” associated with it. Whenever convenient, I prefer to work with slugs because they’re easy to remember, and easier to find. In your WordPress dashboard, navigate to the categories section by clicking Posts -> Categories.

Now find the category you want to exclude. The slug will be next to it under the appropriate column as shown here:

Get Slug of Category

Usually, it’s just a lowercase version of the category name – but it doesn’t have to be.

Adding Code

Once you have the slug, paste the following code into WordPress. If you don’t know how to do this, read my earlier tutorial on how to paste snippets into WordPress.

Here’s the code:

function disable_category_search($query) {
if ($query->is_search) {
    $cat_id = get_cat_ID('personal');
    $query->set('cat', '-'.$cat_id);
}
    return $query;
}
add_filter('pre_get_posts','disable_category_search');

Replace ‘personal’ with the slug name you obtained in the previous step, save your changes and you’re done! Here you can see the same query no longer shows the same post because it belongs to the “Personal” category:

Search Result Excluded

Now for multiple categories.

Preventing Multiple Categories from Showing Up

Technically, we can tweak the code above a bit to include more than one category, but it’s a bit clumsy. Instead, we’ll just use category IDs instead of slugs. You can obtain the category ID by hovering your mouse pointer over the “Edit” button in the WordPress category screen like this:

Hover and Get the Category ID

The URL in the status bar will show you the “tag_ID” parameter containing the category ID. In this case, it’s “4”.

So first get the category IDs of all the categories that you want to exclude from search. Then paste the following code into functions.php, or a custom PHP code file:

function disable_category_search($query) {
if ($query->is_search) {
    $cat_id = get_cat_ID('personal');
    $query->set('cat', '-3,-4');
}
    return $query;
}
add_filter('pre_get_posts','disable_category_search');

Replace “-3,-4” with your category IDs. Note that these are negative numbers, since we’re telling WordPress to exclude them instead of including them. So my category numbers of “3” and “4”, become “-3” and “-4” respectively.

Save your changes, and now your chosen categories will no longer appear in your search results!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

Joomla vs. WordPress: Which One Is Better?

In this post, we’ll consider five differences between Joomla and WordPress and how you might go forward with setting up a WordPress website.

Blogger vs. WordPress: Which One to Use?

Let's compare WordPress and Blogger across five key areas. Then, we'll show you how to set up a professional blog with WordPress.

How To Back Up Your WordPress Site (And Why You Should Do It Today)

In this post, we’ll take a closer look at why backups matter. We’ll also show you different ways to back up your WordPress website.

How to Eliminate Render-Blocking Resources in WordPress (& Why You Should)

Many things can slow down your site, including render-blocking resources. These are CSS and JavaScript files that delay the loading of content on your site. As a result, visitors will have to wait longer to interact with your page, which could lead to frustration and instant exits.  So, what can you do to eliminate render-blocking […]

Reader Interactions

Comments

  1. Warszawa says

    October 12, 2022 at 6:28 am

    This is what I was looking for. I wanted to exclude 1 category from the search results. There is no such feature inside wordpress itself. Now I know how to deal with it. It looks like adding the code to the file – will it disappear when updating the theme? Will you need to add it every time you update your theme files?

    Reply
    • Bhagwad Park says

      October 12, 2022 at 12:15 pm

      You have two options:

      1. Create a custom plugin for code (recommended): https://www.namehero.com/blog/where-to-put-custom-code-in-wordpress-hint-use-a-plugin/

      2. Use a child theme for your functions.php file: https://www.namehero.com/knowledgebase/157/How-To-Create-A-Child-Theme.html

      Reply
  2. Laurie says

    January 5, 2024 at 1:26 pm

    great instructions and so easy thank you 🙂

    Reply
  3. Dan Miller says

    March 20, 2024 at 10:20 pm

    In the second code snippet (preventing multiple categories), you have entered the category ids.
    In that case do I need the get_cat_ID line?
    $cat_id = get_cat_ID(‘personal’);

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA