1

On my wordpress search page I would like to show a 'total found results'. This needs to be shown at the top of the page in a heading.

I've tried this code but I gives me only the results of the specific page. What I want is all the results, not just the page.

<?php echo $wp_query->post_count; ?> 
  • 2
    echo $wp_query->found_posts. Please note, if you've used query_posts somewhere, then the main query object will be broken – Pieter Goosen May 8 '15 at 12:08
  • dont forget global $wp_query; before calling. or use $GLOBALS['wp_query']->post_count; – T.Todua May 8 '15 at 12:20
  • @taztodgmail You only need to global $wp_query if the template was loaded in an unconventional method. Otherwise it will always be available (see load_template()) – TheDeadMedic May 8 '15 at 13:27
2

If you check the documentation for WP_Query you will notice that post_count, which you are using, returns the number of posts being displayed. That is what you see, but not what you want. found_posts returns the total number of results for the query, which is what you want.

$post_count
The number of posts being displayed.

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

Note that if your query runs with 'no_found_rows' => true the found_posts value will be 0, and there is a found_posts filter that can effect the result as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.