Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for a way to check how many posts are returned by a specific loop. So if I have a loop like this..

 $featured_post_id = get_the_ID();
 global $post;
 $args = array('cat' => '9', 'posts_per_page' => 5, 'post__not_in' => array($featured_post_id) );
 $my_query = new WP_Query($args);
 if ( $my_query->have_posts() ) : while ( $my_query->have_posts() ) :   $my_query->the_post(); ?>

And if this loop returns less than 5 posts then I want to show something. In my situation I'd be showing another loop to add more results.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Try this:

if ($my_query->post_count < $min_posts) {
    // Do Something.

Good luck! Hope that helps.

share|improve this answer
Thanks perfecto! – Pollux Khafra May 21 '12 at 20:38
@PolluxKhafra You probably want to use found_posts instead of post_count, as post_count maxes at what posts_per_page is set to. – mor7ifer May 21 '12 at 20:43

Your Answer


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.