WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Quick post count using WP_Query( $args )? (3 posts)

  1. ixwa
    Member
    Posted 2 years ago #

    Trying to get a total count of all posts matching a query before running the loop. I know it's possible to first run a loop with $i++ to tally the results, but this means running the loop twice (presumably slower).

    Anyone know if there's a quick WP_Query() parameter for getting the equivalent of a mySQL COUNT() during SELECT?

    Thanks.

  2. vtxyzzy
    Member
    Posted 2 years ago #

    Do you really need the count before the query, or just before you display the results?

    If the latter, you can just use the found_posts property of the query. For example,

    <?php
    $args = array(
       'posts_per_page' => 5,
       'post_type' => 'post',
    );
    $myquery = new WP_Query($args);
    
    echo "<h2>Found: $myquery->found_posts</h2>";
    
    ?>
  3. ixwa
    Member
    Posted 2 years ago #

    Actually no, this is exactly what I was looking for. I needed the full count of matching results before starting the loop.

    Thanks very much.

Topic Closed

This topic has been closed to new replies.

About this Topic