Select Which Categories Show in the WordPress Sidebar Widget

Animation. The middle of three categories hides, then returns, illustrating the ability to select widget categories for showing and hiding in WordPress.

WordPress offers a nice Categories widget for sidebars and footers right out-of-the-box. However, while this widget is simple and light-weight, it provides little customization options. Fortunately, a simple, light-weight coding solution enables developers to select widget categories.

The ability to select widget categories is helpful when developers like me create Custom Post Types (CPTs) with their own categories, in addition to whatever categories I make for the blog posts.

(Want to learn how to make your own WordPress Custom Post Types? Check out my two methods: How to Make Custom Post Types with Elementor Pro and How I Manually Added a Custom Post Type in WordPress with PHP and ACF.)

The WordPress Categories widget, however, doesn’t distinguish the two. Out-of-the-box, there’s no way to separate CPT and blog categories.

Screenshot. Before selecting which widget categories to hide/exclude, all the Custom Post Type (CPT) categories also displayed with the blog categories in the WordPress sidebar widget.
A “before” screenshot showing blog and Custom Post Type (CPT) categories alike show in the WordPress sidebar widget. The categories I want to exclude (the CPT’s) are highlighted in yellow.

Several 3rd party WordPress plugins can solve this problem by offering to apply conditionals to the WordPress widget or by simply sharing a whole new Categories widget. For example, WP Categories Widget and Display Categories Widget. On one hand, 3rd party plugins are a quick and easy solution. On the other hand, 3rd party plugins add weight to your website too. I feel silly installing a whole new plugin and maintain it just to add one small feature to my website.

Coffee Cup Web [domain down] shares this code solution that’s nearly as quick and easy as installing a 3rd party plugin though! Simply open your WordPress functions.php document, and copy-paste the code below.

The Code to Hide Widget Categories

Open your child theme’s functions.php file, and insert the following lines of code:

//Hide categories from WordPress category widget
function exclude_widget_categories($args) {
   $exclude = "1, 4, 8, 57, 80";
   $args["exclude"] = $exclude;
   return $args;

add_filter("widget_categories_args", "exclude_widget_categories");

This codes defines a WordPress function to exclude categories from its native widget. Specifically, the categories you specify are hidden. Then, the code executes the function applying the specified settings.

Updated screenshot after excluding CPT categories from the WordPress Categories sidebar widget.
An “after” screenshot illustrates that the WordPress Categories widget no longer displays selected four CPT categories.

How to Identify Widget Category Numbers

Like most WordPress elements such as pages and posts, categories also receive a unique ID. This is perhaps the easiest way to find these numbers:

  1. In the WordPress sidebar, hover over Appearance and select Widgets from the pop-out contextual menu.
  2. Drag and drop the Categories widget from the list of Available Widgets on the left side of the screen into a location on the right side of the screen, such as Default Sidebar.
  3. Apply any settings, such as the widget title, dropdown, hierarchy, and/or post counts.
  4. Click Save to apply the changes to the WordPress website.
  5. Preview a page with the Categories widget.
  6. Right-click somewhere on the web page, and select Inspect from the Developer’s tools to look at the code for the website elements on hover.
  7. Hover over a list item from the WordPress Categories widget for a category you wish to exclude and take note of the number that appears in its CSS class.

The code for a widget category looks something like:

<li class="cat-item cat-item-8">...</li>

Grab the number shown beside the cat-item class. That’s all that’s needed to find category IDs! Now, return to the code above and add these numbers to the “exclude” list to select widget categories that display.