Posted on 11 Comments

Don’t show a WordPress category on posts page

Have you ever used WordPress posts for something other than a blog post? I use them for FAQ pages, but I don’t want the FAQs to show up in the main blog, so I added this into the theme functions.php. It can also be used in a new plugin.

(?? = category id number)

function exclude_category($query) {
  if ( $query->is_home )
    $query->set('cat', '-??');
  return $query;
}
add_filter('pre_get_posts', 'exclude_category');

The minus sign in front of the category id means don’t include.

11 thoughts on “Don’t show a WordPress category on posts page

  1. Thank you Steph for the code. Will use it since i want to keep some categories away from the home page. I was not very sure where to add this code though figured out later! Maybe not many people are WordPress-stupid as I am…
    Shyam

  2. Hiya, Thanks for the code, it works perfectly! I was wondering if you also knew how to keep certain categories out of other parts of the website like recent post widgets at the footer and sidebar, etc. Thanks again for your post, best,
    Allen

  3. Thank you so much for sharing this knowledge. It worked perfectly for my needs and saved me a huge headache. Keep up the great work!

  4. Thanks Steph for sharing – huge help! Working on redesign of my site and this helps me a lot!

  5. Steph, I had some problems with defining multiple categories using above method and found this (similar) solution – thought I’ll share here: http://codex.wordpress.org/Function_Reference/query_posts

    I used query_posts( ‘cat=-1,-2,-3’ ) but instead of making new function I placed it in index.php

  6. great function …. thank you for sharing

  7. Ahh… Nope!

    This blew up my database!

    Thanks for that!!

    MWH

  8. thanks for the tip, I just wanted to say that there is a more simple way to don’t show a page in the main blog. You can, for example, create your own menu, then your FAQ/else.. page and exclude it from the menu, so it’s accessible but not in the nav bar

  9. this is wrong:
    function exclude_category($query) {
    if ( $query->is_home )
    $query->set(‘cat’, ‘-??’);
    return $query;
    }
    add_filter(‘pre_get_posts’, ‘exclude_category’);

    should be:
    function exclude_category($query) {
    if ( $query->is_home() ) //is_home is a function
    $query->set(‘cat’, ‘-??’);
    return $query;
    }
    add_filter(‘pre_get_posts’, ‘exclude_category’);

  10. Great! Thanks a lot!

  11. Is there any plugin to hide category name instead of code.?

Leave a Reply

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