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.

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:

<?php
if (is_home()) {
query_posts("cat=-1,-2,-3");
}
?>

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:

<?php
if (is_home()) {
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("paged=$page&cat=-1,-2,-3");
}
?>

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


13 Comments

Seth

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!

09 07 2009 11.44AM
Alex

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!

20 07 2009 07.51PM
Mael

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

29 07 2009 03.44PM
richr

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

07 10 2009 03.29PM
Gus

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…

10 10 2009 07.56PM
il maistro

Extremely helpful! thanks!

23 10 2009 10.28PM
elena

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

25 11 2009 01.12PM
Sandy-Lee

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!

29 06 2010 05.54PM
lextergroove

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

31 10 2010 01.54PM
craigblaze

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

CURRENT CODE – EXCLUDE CATEGORY GLOBAL:

24 11 2010 02.35PM
J

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 🙂

25 01 2011 05.44AM
Roscoe

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

04 04 2012 08.55PM
Uber Gardenr

Thanks, this is very useful. I have been living with a non functional nav for a whole year.

The navigation worked on all pages except the home page. This code solves everything perfectly.

26 01 2014 01.54AM

Leave a Comment

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

Let's start a project