Get post count of current loop when using multiple queries on one page

$wp_query hold main loop of page and should not be used to create multiple loops.

If you are using new WP_Query object then your variable that holds it will have according count:

$my_query = new WP_Query();
// stuff
$count = $my_query->post_count;

If you are using get_posts() then WP_Query object is not accessible and you should just count returned set:

$posts = get_posts();
$count = count($posts);

Leave a Comment