Excluding Categories from Different Parts of WordPress

Update January 18, 2016: Thought I would mention that if you also want to remove the categories from the Archives drop-down, check out my How To Exclude Categories in WordPress Archives Drop-down post.

Update January 24, 2015: I discovered a bug with the code below. I added an is_admin() exception so the post listings in the admin section would still work properly. Without the exception, the Category filter would fail to work. I actually created a WordPress Support thread when I noticed it.

For the longest time I had been using the Advanced Category Excluder plug-in to keep a couple post categories from showing on the blog’s home page, RSS feed, search, and monthly archive views. Well, after my webhost upgraded the server to PHP 5.3.17 from PHP 5.2.X it broke that plug-in. After searching around for some answers, I tried out the Simply Exclude plug-in which did work; however, it had a couple issues. One problem was that if I excluded a category from showing in the archive view or the category drop-down widget, it broke the permalink for categories where the address bar would show /?cat=6 instead of /category/dentist/ for example. It also excluded the category from both the monthly archive view and the category view itself. I did some more searching around and came across this WordPress Codex article about Category Exclusion. I adapted the code there and placed the following 8 lines of code into my theme’s functions.php file (info on using a theme’s functions.php):

function remove_my_categories( $wp_query ) {
  // 61 = Daily Tweets, 74 = Testing
  $remove_cat = '-61,-74';

  // remove from archives (except category archives), feeds, search, and home page, but not admin areas
  if( (is_home() || is_feed() || is_search() || ( is_archive() && !is_category() )) && !is_admin()) {
    set_query_var('cat', $remove_cat);
    //which is merely the more elegant way to write:
    //$wp_query->;set('cat', '-' . $remove_cat);

add_action('pre_get_posts', 'remove_my_categories' );

That’s all I needed to exclude those categories’ posts from showing on the home page, RSS feeds, search results, and the monthly archives! I can still select the category from the category drop-down list and view it in the category context. Needless to say, I’ve removed both of those plug-ins from my blog.

Hopefully this information will help some others out that run into a similar situation. 🙂

7 thoughts on “Excluding Categories from Different Parts of WordPress

  1. john

    Spent a good hour looking for away to remove specific categories from the archive list widget, then found this.
    A quick bit of hacking and it works. Great job.

  2. Denis

    Thanks for this code example, I have a website which has a “thought of the day” daily post category that I needed to exclude from the normal “news” blog roll and archives. So I used the code below based on your example code. It works for me except for one issue; The wordpress Archive Sidebar widget generates a new months archive at the beginning of a new month based on the daily “though of the day” post even though this category is hidden from the archives.

    When a user clicks on the new archive month in the sidebar, an error is generated as there are no posts to display. How can prevent monthly archives from being generated for months that only contain my excluded category.

    I only want an archive month index to be generated in the sidebar when there are blog posts other than my excluded “though of the day” category in the that month.

    Thanks very much for any help, hope this makes sense.

    Here is the code I am using:

    function remove_my_categories( $wp_query ) {
    // 31 = thought of the day
    $remove_cat = ‘-31’;

    // remove from archives (except category archives), feeds, search, and home page, but not admin areas
    if( (is_home() || is_feed() || ( is_archive() && !is_category() )) && !is_admin()) {
    set_query_var(‘cat’, $remove_cat);
    //which is merely the more elegant way to write:
    //$wp_query->set(‘cat’, ‘-‘ . $remove_cat);

  3. Joseph


    How can I “hide” or exclude Posts in the Post Admin list based upon Categories they are assigned to?

    For my immediate purposes, I only need Posts that are assigned to a single Category to be presented to me.

    Once all the Posts are assigned to more than one category (manually by me), I should be presented with no posts in my Posts Admin Pane.