Categories
WordPress

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 replies on “Don’t show a WordPress category on posts page”

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

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

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

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’);

Leave a Reply to Youssef Cancel reply

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