How to Hide WordPress Categories

How to Hide WordPress Categories: In the following simple solution, I show you how to keep a selection of categories from displaying on your WordPress pages and posts. This can be useful if you no longer wish to use or display specific categories on your site, but have already been indexed for the category content from search engines such as Google and Bing. Upon completion of this tutorial, the permalink structure of the hidden categories are still made available to the search engines, however, the categories you have chosen to hide are no longer prominently displayed on your blog to the visitor.

Hiding WordPress Categories

To hide WordPress categories, you can use a combination of custom code in your theme's functions.php file and possibly some CSS if needed. Shown below are some common methods to achieve this.

Before getting started, you'll want to locate the category IDs you wish to hide. To find WordPress category IDs, go to Posts > Categories in the WordPress dashboard. Then, simply hover over the category name and locate the cat_ID in the URL at the bottom of your browser.

WordPress cat_id
cat_ID=#

Hide WordPress Categories in the Loop (Query)

If you want to exclude certain WordPress categories from the main query (e.g., the blog page), you can use the pre_get_posts action hook:

  1. From the WordPress Admin Dashboard, navigate to Appearance > Theme File Editor.
  2. Open your theme's functions.php file, and add the following action hook to the bottom of it:
    function exclude_categories_from_blog($query) {
    if ($query->is_home() && $query->is_main_query()) {
    $query->set('cat', '-1,-2'); // Replace -1, -2 with the category IDs you want to exclude
    }
    }
    add_action('pre_get_posts', 'exclude_categories_from_blog');

Hiding WordPress Categories in Widgets

If you'd rather exclude WordPress categories from the category widget only, you can use a filter function instead:

function exclude_widget_categories($args) {
$exclude = "1,2"; // Replace 1, 2 with the category IDs you want to exclude
$args["exclude"] = $exclude;
return $args;
}
add_filter('widget_categories_args', 'exclude_widget_categories');

Hide WordPress Categories from RSS Feeds

To hide WordPress categories from RSS feeds, you can use a similar approach with pre_get_posts:

function exclude_categories_from_feed($query) {
if ($query->is_feed) {
$query->set('cat', '-1,-2'); // Replace -1, -2 with the category IDs you want to exclude
}
}
add_action('pre_get_posts', 'exclude_categories_from_feed');

Hiding Categories from Category List Output

If you want to hide certain categories when listing categories (e.g., in a category list), you can use a custom function:

function wpb_exclude_category($wp_list_categories) {
$exclude = array(1, 2); // Replace 1, 2 with the category IDs you want to exclude
foreach ($exclude as $cat_id) {
$wp_list_categories = str_replace("cat-item-$cat_id", "hidden-cat", $wp_list_categories);
}
return $wp_list_categories;
}
add_filter('wp_list_categories', 'wpb_exclude_category');

Hiding Categories with CSS

In some cases, you may want to hide WordPress categories with CSS (e.g., if they are displayed in a widget or menu):

  1. Add the following to your themes CSS file, replacing 1 and 2 with the category IDs you want to hide:
    .cat-item-1, .cat-item-2 {
    display: none !important;
    }

By implementing one or more of these methods, you can hide specific categories from various parts of your WordPress site. Make sure to back up your functions.php file before making any changes, as errors in this file can break your site.

Easily Hiding Categories from WordPress Sidebar

You can also use a plugin to hide WordPress categories from the sidebar. Here's how:

  1. Download, install and activate the Sidebar PHP Code Widget Plugin
  2. Navigate to Posts > Categories and hover over the name of the category you would like to exclude and get the Cat_ ID number from the browser status bar.
  3. Navigate to Appearance > Widgets
  4. Drag PHP Code from Available Widgets to your Current Widgets
  5. Edit the PHP Code Widget and add the following code (replace 1,2,3 with the category IDs you would like to exclude):
    <ul>
    <?php wp_list_categories('orderby=name&exclude=1,2,3&title_li='); ?>
    </ul>
  6. Click Done and then click Save Changes
  7. Go view your site!