How can i get the row counts in query post like mysql count(*).

$obj_name = new WP_Query($args);

while ($obj_name->have_posts()) : $obj_name->the_post(); 

// here i want to predict looping counts


How can i do this.

3 Answers 3

$num = $obj_name->post_count; 

Reference: wp_query

  • 6
    This does not actually answer the question - post_count will return the total number of posts being shown on the current page.
    – Ryan
    Mar 6, 2020 at 17:08

The accepted answer here is wrong which is also confirmed in my situation. Please, compare from the referenced page:

$post_count The number of posts being displayed.

$found_posts The total number of posts found matching the current query parameters

This way, $post_count will show, for example, number of posts per page if there are more than one page of results. Only if total number is less than number of results per page it will match total amount.

The correct method to get total results number is:


  • 3
    This should be the accepted answer
    – Jean R.
    Oct 28, 2020 at 10:38

To get the total number of posts WP_Query returns use "found_posts"

Here is the example -

           $args = array(
           'post_type' => 'post'
        $the_query = new WP_Query( $args );
        $totalpost = $the_query->found_posts; 

Use your custom post type name in place of 'post', you can also pass the category id too ( 'cat' => 4,)

  • 1
    Watch out if you use posts_per_page in your query, this will return the total post count regardless of page size
    – Boltgolt
    Sep 7, 2021 at 14:52

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.