Why would in_category only return one post in a specific category?

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(); ?>
    <?php
        if(in_category('7')) {
        the_title();
    }
    ?>
<?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.

Read More

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?

Related posts

Leave a Reply

2 comments

  1. it should be

    in_category(7)
    

    integer, not string.

    but a better way to do this is:

    query_posts('cat=7');
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    endwhile;
    endif;
    
  2. 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:

    query_posts('cat=7');
    

    just above your loop before this line:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>