WordPress How-To: Exclude a Category from the “Categories Widget”

How-to exclude certain categories from displaying in the “Categories Widget” in WordPress 4.1.

There are instances where you may have a category that you do not want the default WordPress “Categories Widget” to display. The following short code sample will quickly eliminate the listed categories from displaying. (In the default “Categories Widget” only)

WordPress suggests that you place the following code in your theme’s functions.php file. This is not an ideal location because you will have to re-edit the file every time you update or change your theme. One suggestion is to use a plug-in like Add Actions And Filters that will allow you to maintain your action and filter edits separately from any themes. Alternatively, if you are developing your own plug-in, place the code in your plugin’s root file.

Notice on the highlighted $exclude  assignment line, I have the values "9,10". Place your own category ID(s) to exclude here instead.

Here is a slightly more advance version:

First, you’ll notice I’ve added an is_admin conditional if statement. Although the widget is not typically viewed on the admin page, this simply future proofs the code.

You’ll also notice I’ve added an apply_filters function that will allow end users the ability to hook into your plug in to add their own categories to exclude.

Lastly, I’ve included the ability to detect whether the widget already had a set of categories to exclude. If there are, merge the new set with the old set of excluded terms.

Once again, this will only exclude one or more categories from displaying in the default WordPress “Categories Widget”.

To exclude certain categories’ from displaying in the “Archives Widget”, check out WordPress How-To: Exclude a Category from the “Archives Widget”.

To exclude certain categories’ from displaying in the “Recent Posts Widget”, check out WordPress How-To: Exclude Posts from the “Recent Posts Widget” by Category.

To exclude certain categories’ posts from displaying check out WordPress How-To: Exclude Posts by Category.

To exclude certain categories’ posts from displaying in posts’ “Previous/Next” links check out WordPress How-To: Exclude a Category from the “Previous/Next” Links.

Posted in WordPress Tagged with: , , , ,
2 comments on “WordPress How-To: Exclude a Category from the “Categories Widget”
  1. Vincent says:

    Hi,
    Thanks for this plugin.

    However, since the update to WordPress 4.4, the plugin doesn-t work anymore…

    Have you an idea to solve it ?

    Tanks by advance.

    • GrokDD says:

      Vincent,

      Thank you for taking the time to look over the plugin.

      I have updated both my development site and live blog site to WP 4.4, with both versions of the plugin above (cut and pasted exact copy), changed the exclude array variables to my category IDs. Both versions worked fine.

      Remember this only removes the categories from the “categories widget”.

      The next step (if you can) is to deactivate all other plugins and see if something is conflicting.

      Send a link to you site, and we’ll work on it together!

      Hopefully we can get it working again.

Leave a Reply

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

*