Filter Categories from Homepage

You’ll often have post categories for things like “Events” that you don’t want showing up on your homepage with all your other posts. This is the easy way to filter them out.

We’ll use the pre_get_posts hook to hop in right before the posts are pulled from the database and modify the query. For more information, see my post on customizing WordPress queries.


Receive New Posts by Email


  1. Bill, thanks for this. Will this work for Thesis 1.7? Also would you have a tutorial that shows how to change the standard 2 column layout for teasers into a 3 or 4 column layout and to only show the post image rather than the exerpt with the post image.

    Please shoot me an email if you do.

  2. OMG Bill, ur a god! i had to separate my editorial stuff from the seriou stuff. U rock!!!
    i love how u break the code down and xplain it!!!

  3. Nice post Bill. There is also another, easier way to do this:

    function exclude_cats_from_home($query) {
    if ( $query->is_home) {
    $query-> set(‘cat’,’-32′);
    return $query;

    1. Good catch. I should go through my older posts and update them sometime 🙂

      This is the approach I take now too. This is also useful for controlling the number of posts that show up on a page (ex: if you have a Links post type and want 30 per page, but don’t want 30 blog posts per page).

  4. ‘pre_get_posts’ is a native WordPress filter. It runs before a query is executed in the main query or any instance of WP_Query, such as query_posts, get_posts, or get_children. Using ‘pre_get_posts’ eliminates the need to use Thesis-specific / Genesis-specific hooks.


  5. I can’t get this one to work, or Michael’s solution either.

    I’ve entered this:

    function home_filter_begin() {
    global $wp_query;
    $exclude = '-42';
    query_posts(array_merge(array('cat' => $exclude ),$wp_query->query));

    function home_filter_end() {

    And yes, that is the right ID for the category I want to exclude. Does it matter that I have a different blog page than my front page?

  6. Oh, nevermind. I found this on another site, must have been a problem with my cut and paste of michael’s code:

    function exclude_cat($query) {
    if ( $query->is_home) {
    $query-> set('cat','-42');
    return $query;

    Also, Bill, you have a code tag in your code, makes for errors on those copying and pasting into their php files 😉

    1. Thanks for catching that. I recently updated my site and went through all blog posts converting < code > and < pre > to the syntax highlighter above, and there’s still a few posts with issues in them (like this). Hopefully I’ll find all the issues soon.

        1. Good catch. I just updated the snippet (might take a day or so for the post to reflect it). I also replaced $wp_the_query with $query->is_main_query() since that was added in 3.4.

    1. This should still work if your blog isn’t the front page. is_home() applies to the blog page (whether or not it’s your homepage), and is_front_page() applies to the static front page.

  7. OMG!!!!!!

    I’ve been searching for two days on how to EXCLUDE a certain CATEGORIES on my home page and this piece of code done it for me.

    I’ve just put the code on my custom.php of my Thesis 2.1 classic responsive theme and it works like a charm!


Leave a comment