I tried tplacing this:

    <?php $count = count($custom_posts); ?>
    <h2><?php echo $count; ?></h2>

at the end of the loop:

      <?php if ( bbp_get_forum_title() == 'Test Forum 1' ) : ?>
            <?php $custom_posts = new WP_Query(); ?>
            <?php $custom_posts->query('post_type=blocks&location=Business and Finance&order=DESC'); ?>
            <?php while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?>
                <div class="ad">
                    <?php the_content(); ?>
                <?php $count = count($custom_posts); ?>
                <h2><?php echo $count; ?></h2>
            <?php endwhile; ?>
      <?php endif; ?>

But instead of the total of posts, I getting this output:

Translation 1

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim 1

Any suggestions to fix this?

3 Answers 3


Correct way of getting the total number of posts is:

<?php $count = $custom_posts->found_posts; ?>


Edit: acknowledging @Kresimir Pendic's answer as probably correct. post_count is the count of posts for that particular page, while found_posts is the count for all available posts that meets the requirements of the query without pagination. Thank you for the correction.

  • Thanks! Hey one last question. How can I use that number to make an if statement which is out of that loop (before of the loop). Because it seems like the number is only displayed when I place the variable after that loop.
    – wyc
    Aug 28, 2011 at 14:02
  • 7
    You can put the $count = $custom_posts->post_count just after the $custom_posts->query(). Note that $custom_posts->post_count only gets you the number of results in that 'page' of the result set. If you need to get the total number of results in the 'whole' result set, use $custom_posts->found_posts. Jul 29, 2016 at 0:34
  • 3
    This answer is most likely not correct for most situations. Use found_posts (all found posts) instead of post_count (number of posts to display on this page). This comment is redundant logically speaking, but not socially speaking. Dec 23, 2017 at 11:43
  • 2
    This answer is incorrect. $custom_posts->post_count will return the amount of posts shown on this page, so it will display either the posts_per_page value of the query or a lower value if the amount remaining to show is lower. the correct answer should be <@kresimir-pendic>'s answer that uses $custom_posts->found_posts Mar 12, 2018 at 14:13
  • Life saver. Till now I have been using another query to find posts without pagination
    – melvin
    Dec 10, 2021 at 14:53

Manny linked correct documentation page but post_count is wrong. To get total number of posts WP_Query returns use "found_posts"


// The Query
$query = new WP_Query( $args );
$total = $query->found_posts;
  • 3
    This one should be the accepted answer.
    – Christine Cooper
    Feb 6, 2018 at 15:49
  • 1
    This is absolutely the right answer. Mar 12, 2018 at 14:13
  • 1
    I also reconfirm that this the correct answer. This should be accepted. Jun 21, 2019 at 6:43
  • 1
    In confirming the confirmation of the most recent confirmation I have determined that the original confirmation is indeed confirmed, as is the confirmation after that one.
    – user38365
    Aug 18, 2020 at 20:53
  • 1
    @Bysander stated that he "can confirm the confirmation..." but did not actually confirm. Therefore, I write to confirm that I am unconfirming the confirming of his alleged confirmation. Oct 11, 2020 at 12:47

Or this:

$query = new WP_Query( $args );
$count = $query->post_count;

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.