WordPress: Exclude category from home page breaks post navigation

Here at Kino Creative we use WordPress a lot as a cost effective (ie. free!) content management system (CMS) . WordPress is very flexible if you are prepared to get your hands dirty with the template code but being open source the quality of documentation can be a little patchy.

One of the most common queries is how to exclude posts from certain categories from appearing on the homepage. It is actually very easy when you know how. Your homepage template (index.php) will include what is known as “the loop”, and this is the part that pulls in the content from the database. It should look something like this:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
(Stuff that displays the posts)
<?php endwhile; ?>
<?php else : ?>
(Stuff to show if no posts were found)
<?php endif; ?>

To exclude categories you need to add a query before all that to tell WordPress what to exclude:

if (is_home()) {

The above example taken from the WordPress Codex will return all posts except those in categories with the id 1, 2 and 3. The easiest way to find out your category id’s is to install the “Reveal IDs for WP Admin” plugin.

What the WordPress Codex and virtually all of the tutorials I have seen don’t tell you is that this will break the post navigation. By post navigation I mean the older/newer post links that show if you have more posts than the number set to be displayed per page in the WordPress dashboard. With these navigation links broken, clicking them will simply display the same page rather than showing the older or newer posts.

To fix the post navigation you should amend the above code to this:

if (is_home()) {
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;

This addition lets WordPress know that this page is should be paginated so it will enable the older posts/newer posts links correctly.

13 Responses to “WordPress: Exclude category from home page breaks post navigation”

  1. Seth says:

    Nice post, thanks! It’s not easy to figure out some of the vagueries of WordPress, and this article clears a few things up. Well laid out too I might add!

  2. Alex says:

    I had been searching for something like this for hours until I stumbled upon this post. WordPress documentation, which doesn’t even acknowledge that the problem exists, has such a frustrating number of holes. Thanks for sharing your knowledge!

  3. Mael says:

    ++1 !
    I found your post at the second page of results in google,after reading wrong and incomplete explanation during hours !
    Thank you again !

  4. richr says:

    This has really helped me exclude some categories on a wordpress powered site. Thanks

  5. Gus says:

    I’ve been looking for this for over a week now, and found nothing but wrong code while doing it. Thank you!!! Thank you so much for this!!!
    Cheers from Brazil!!
    PS: Thanks for soccer, lots of fun…

  6. il maistro says:

    Extremely helpful! thanks!

  7. elena says:

    Thank you! I have been looking and looking for that! So simple, so helpful. BR!

  8. Sandy-Lee says:

    Thank you! You have saved my butt!

    I’m a total newbie to WordPress and have had to finish off a half baked project started by a wordpress ‘pro’ who couldnt get this working!

    Thank you so very much!

  9. lextergroove says:

    Thanks for this clear and simple explnation really helped me, i wonder why it hasn’t be included in the wp-codex…

  10. craigblaze says:

    How do you exclude category posts for one individual page that is not the home page and not global?


  11. J says:

    Thank you!!!! I was so wondering about this! And yes, no one mentions it like they have never seen that problem?!
    Thank you once again :)

  12. Roscoe says:

    YES!! I can’t stand how query posts breaks the pagination.
    Thank you for this simple fix.

Leave a Reply

Posted on July 9th, 2009
by Kino Creative in Hints & Tips