1

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..

<?php
 $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.

1 Answer 1

3

Try this:

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

Good luck! Hope that helps.

1
  • 4
    @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
    Commented May 21, 2012 at 20:43

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.