Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

Display Search Term and Result Count in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
Display Search Term and Result Count in WordPress

In this tutorial we will share how you can add a simple function on your search page which will display the search term and the number of results. This feature was a special request by one of our user via email. If you want us to cover a topic, then feel free to make a suggestion.

Displaying search term and result count in WordPress search

Open your search.php file in your theme and add the following code:

<h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' &mdash; '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>

The code above will display something like this:

Search Result for twitter — 15 articles

You can also highlight the search term by adding .search-terms CSS class to your theme’s stylesheet. Here is a simple CSS to get you started:

.search-terms {

This is just one of the cool things that you can do for your Search Page when customizing it. You can also highlight search terms in the results, and even add a search by category feature to your WordPress search.

Source: Michael Martin

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. Aniruddh says:

    Can I use this code in the sidebar.php file? As I need to show the search term and result count in the sidebar. Please help.

  2. Marco says:

    thanks a lot for this snippet! :)

    according to the wp codex the function wp_specialchars is deprecated. it says that you should use esc_html instead.


    • Marco says:

      Next time I take a close look to the comments. CHADHAYTON already posted a link how you can handle it now.

  3. Henry says:

    echo $wp_query->found_posts;
    Adding this to the top of your search page will display the number of posts found for a particular search.
    echo get_search_query();
    This will display the search term (entered in to the search box by your site visitor).
    This will display the time it took to run the search.
    All together you could output something like:
    Your search for “coffee beans” returned “16” posts in “0.87” seconds.

  4. chadhayton says:

    It is hard to tell how old this post is, but WordPress 3.1 includes a way to get the total search results that doesn’t require a separate query:

  5. Chris Murphy says:

    Wow. It’s a lot simpler than that to count WordPress’ search results, here:

        global $wp_query;
        $count = sizeof( $wp_query->posts );
        //var_dump($wp_query);// The WP Query Object
        var_dump($wp_query->posts); // The WP Query Object's 'posts' property (note the plural)

    If you drop that snippet in your search template, it will output the properties of the ‘$wp_query’ variable, specifically the ‘posts’ property of the object. This is an array that stores a collection of the returned posts from the search query, which you can do a number of things with once you access it, including (*drumroll*), *count* the number of posts in that array using either PHP’s ‘count()’ or ‘sizeof()’ functio

    • Editorial Staff says:

      Your code is cut off a little. Is there a way that you can email us the snippet and we can try it and write about it. Obviously full credit will be given to you.

    • Ilya says:


      This won’t work. The point is to find out the total number of results matching the query — not the number currently fetched. If there are more results than “fits” within a page, your code will only get number of elements on a page.

  6. pete says:

    Search results are a personal thing from a user so it’s good if they are given lots of info for them.

  7. basilakis says:

    So the code is right or not? :)

    I see Konstantin has some point about the query, but the comments “ate” the results! :D

    • Editorial Staff says:

      This code is correct. It works, the only issue he is pointing out is that it can be done differently as well. We have used this code on a client’s website and it works.

  8. Konstantin says:

    Good idea. Bad execution.
    Why in the world would you want to translate a span tag???
    And my favorite: _e(''); Huh??
    Why would you want to query the same search twice?
    Doesn’t make sense at all.

    But I don’t want to hate, this is how it should be done (in my humble opinion):

    post_count); ?>

  9. Tom says:

    I really don’t give my on page search enough thought. Something like this is great because it makes the page more personalised.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.