How to Exclude a Category from Your WordPress Homepage

How to Exclude a Category from Your WordPress Homepage

For one reason or another, you may not want posts from one or more categories appearing in the stream of posts that show up on your homepage. This little bit of code will let you remove whichever categories you like from that stream.

Place the following into your functions file. (Appearance > Editor > Theme Functions – functions.php)

In this example, the numbers 3, 5, and 23 are the category IDs (don’t forget the minus signs in front of them). Choose your own categories to be excluded by finding the category ID. If you aren’t sure how to do that, see this post.
{code type=php}
function excludeCat($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-3,-5,-23’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘excludeCat’);

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

Thanks to wp-snippets for this very handy little piece of code.

Photo credit: Willi Heidelbach

Tags: