Show total number of search results in your WordPress search template

In prepping the new site design for, I wanted to show the total number of search results on my search page.

I started by displaying a count($posts), but since I’ve got my installation showing 5 posts per page, that consistently displayed 5 or less posts. Less than desirable.

So, the next step was duplicating the search query so I could get the count, and as usual I like to try to use the WordPress functions as opposed to touching the database directly:

$tmp_search = new WP_Query('s=' . wp_specialchars($_GET['s']) . '&show_posts=-1');
$count = $tmp_search->post_count;
echo $count . ' results for ' . htmlentities($_GET['s']) . '.';

Unfortunately, the code above still gave me the wrong count. Why? My settings were still set to 5 posts per page; and the WP Query object as assuming this constraint.

The Solution: send posts_per_page=-1 as part of the query.

$tmp_search = new WP_Query('s=' . wp_specialchars($_GET['s']) . '&show_posts=-1&posts_per_page=-1');
$count = $tmp_search->post_count;
echo $count . ' results for ' . htmlentities($_GET['s']) . '.';

…and there you have it. A nice functional search results page. For even further refinement, I’m limiting my search to blog posts, and excluding pages by including post_type=post in my query.


  1. Jermaine Maree says…

    This really came in handy! Was having a similar issue, but it was displaying total posts instead. The posts_per_page=-1 fixed the issue 😀 Thanks!

  2. Reino says…

    Great post! Works for me!

  3. Zoinks! Graphics says…

    Thanks for sharing. I use it to limit whether or not the navigation links display to save page space. This works great!!

  4. Maria says…

    I’m trying to count the number of posts with a particular tag and I’m using the following code:

    echo ‘TAGNAME: ‘ . $my_query->post_count .”;
    wp_reset_query(); ?>

    I’ve tried adding your code in, in various permutations, but I keep getting out either the total number of posts, or the total posts per page each time when the counts are more than the number of posts allowed per page.

    Do you have any suggestions here?

    – Maria

  5. Oncle Tom says…

    Even simpler: <?php global $wp_query; echo $wp_query->found_posts ?> results for the search ‘<?php the_search_query() ?>’

  6. andy says…

    @Oncle Tom — that’s even better, thanks!

  7. Morten says…

    Would love to know the answer to Marias question about tags…


  8. andy says…

    @Maria and @Morten – you should be able ot use @Oncle Tom’s contribution and use the found_posts property from the global $wp_query object:

    global $wp_query;
    echo $wp_query->found_posts . ' post(s) found for '. $tagname;
  9. anu says…

    Please help me, i have search page with pagination but i need to display a drop down also in the same page to select the no of posts to display.Any idea please help me

  10. andy says…

    @anu you should be able to manually create your dropdown, add some Javascript to redirect to a new search URL containing some kind of query string variable for the number of posts to display (let’s call this limit.

    Example URL’s you’re creating, this example searches for “search terms” and shows 15 posts per page:

    global $wp_query;
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
         'posts_per_page' => (int) $_GET['limit'],
         's' => get_query_var( 's' ),
         'paged' => $paged

    This is semi-psuedo code as I just wrote it, haven’t tested or run it – but it should send you in the right direction.

RSS feed for comments on this post. TrackBack URL

Leave a Comment

June 6, 2009

Filed in Development, Wordpress

There are 10 comments »

« Back to the Blog