getting the total count of search results in wordpress

12,709

Solution 1

Try this code,

$allsearch = new WP_Query("s=$s&showposts=0"); 
echo $allsearch ->found_posts.' results found.';

Hope this will helps you.

For more please visit,

Result Count in WordPress

Display Search Result Count

Solution 2

Once you are already in a search query, you can as well use the global $wp_query.

See example below:

<?php
   global $wp_query;
   $not_singular = $wp_query->found_posts > 1 ? 'results' : 'result'; // if found posts is greater than one echo results(plural) else echo result (singular)
   echo $wp_query->found_posts . " $not_singular found";
?>
Share:
12,709
Adarsh
Author by

Adarsh

Updated on June 10, 2022

Comments

  • Adarsh
    Adarsh almost 2 years

    How to get the total count of results, in the search results page in wordpress ... i think my question is clear .i need the total number of search results that displayed in the search results page .And also need to find the count of results from page and post separately
    what i have tried is

        <?php echo count($posts); ?>
    

    by using this i got the total number of search results . but i also need the count of pages and posts in the search results

  • Adarsh
    Adarsh about 6 years
    how to get the count of pages and posts individually
  • Sunil Dora
    Sunil Dora about 6 years
    please check this for the all post counts and for pages pass the value in WP_Query 'post_type' => 'page' wordpress.stackexchange.com/questions/31254/…
  • Mark
    Mark about 4 years
    this solution is much better, because you have all ready the global $wp_query, no need to get it again like in the accepted answer
  • Erik
    Erik over 3 years
    Also for me this solution is the correct one, it's not necessary to make a new query it just wastes time and resources. Please @Adarsh mark this as accepted!