Blog

Sharing WordPress tips and tricks as I find them

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.

15 Comments

  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. Thanks Bill, just the clean simple code answer I was looking for. Works like a champ on Thesis 1.8 and let me remove a plugin I had doing the same functionality. Thanks!
    Kimbelry

  3. 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!!!

  4. 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;
    }
    add_filter(‘pre_get_posts’,'exclude_cats_from_home’);

    • Bill Erickson says:

      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).

  5. ‘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.

    Cheers,
    Michael

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

    I’ve entered this:

    function home_filter_begin() {
    if(is_home()):
    global $wp_query;
    $exclude = '-42';
    query_posts(array_merge(array('cat' => $exclude ),$wp_query->query));
    endif;
    }
    add_action('thesis_hook_before_content','home_filter_begin');

    function home_filter_end() {
    if(is_home()):
    wp_reset_query();
    endif;
    }
    add_action('thesis_hook_after_content','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?

  7. 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;
    }
    add_filter('pre_get_posts','exclude_cat');

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

    • Bill Erickson says:

      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.

      • Hey Bill, I think there’s an error in the code: it says 'cat' => '-5' and I get and “unexpected double arrow” error. I think it needs to just be a comma.

        • Bill Erickson says:

          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.

  8. Any idea how to make this work if the blog isn’t your homepage? I have a static page at ‘home’ and the blog at /blog.

    Thanks!

    • Bill Erickson says:

      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.

  9. Thank you, thank you. I finally got this to work!

  10. 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!

    Thanks.

Join the Conversation

If you'd like to include code in your post, please post it to http://gist.github.com and include a link.