So... what I'm trying to do is simple - get the post content for all posts, but only in a given category. Simple - filter the loop using in_category. So I did this (this is actually a stripped version of the loop I used to try to troubleshoot this):

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        if(in_category('7')) {
<?php endwhile; ?>
    <!-- post navigation -->
<?php else: ?>
    <!-- no posts found -->
<?php endif; ?>
</div>  <!-- main -->

Here's the catch... for category 7 it only returns ONE post (the most recent). For other categories, I get all of the posts in that category. Removing the restriction gives all posts. The posts in category 7 aren't assigned to any other category. Creating a new category and reassigning the posts in category 7 does not work... but creating new posts and assigning them to a new category and then using THAT new category ID does. But twice now it's stopped working.

That code is the entire loop - there's nothing else on the page aside from HTML markup at this point. I can't see why the code wouldn't simply give me all published posts in category 7 (and yes, they are all set to Published and Public). I've got code another page that looks for recent posts in that category and THAT code finds them. I can modify that and use it here, but I'm curious as to what I'm missing here. Thoughts?

2 Answers 2


it should be


integer, not string.

but a better way to do this is:

if ( have_posts() ) : while ( have_posts() ) : the_post();


The problem is that you are using a conditional tag only after querying the posts, so you are getting post from all categories but only displaying the one that is in category 7.

to query (select) only posts from a specific category you need to alter the query using query_posts() something like this:


just above your loop before this line:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  • /headdesk... Thanks guys. I knew it was something simple I'd overlooked. I'd still love to figure out why that code showed all of the posts for other categories but only the most recent for category 7 but... meh. Commented May 17, 2011 at 21:04
  • ammmm did you read my answer ? i started with the reason for your problem.
    – Bainternet
    Commented May 18, 2011 at 4:57
  • @ Bainternet - yep. But the issue was that there are two categories, 6 and 7. Both with 3 posts. The posts in cat6 were assigned only to cat6, same for cat7. If I do nothing more than edit the number from 7 to 6 in my (bad) code, all 3 posts in category 6 appear. Of course, your code works better and is correct, but what was stumping me was that two categories both with several posts (and otherwise the same, i.e. the posts in each category were only assigned to that category) behaved differently using the same erroneous code. Commented May 18, 2011 at 23:14
  • @rick when you get into the loop the query has already been made with the default arguments (unless you change them) so it only selects a number of posts from you database and based on there posting date, and if your category 7 posts were not all there then you want see all of them.
    – Bainternet
    Commented May 19, 2011 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.