Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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?

share|improve this question
add comment

2 Answers

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;
share|improve this answer
add comment

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(); ?>
share|improve this answer
    
/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. –  rick gregory May 17 '11 at 21:04
    
ammmm did you read my answer ? i started with the reason for your problem. –  Bainternet May 18 '11 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. –  rick gregory May 18 '11 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 May 19 '11 at 6:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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