Free Wordpress Blog Setup

Display Search Term and Result Count in WordPress

By Editorial Staff in Tutorials
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.

Open your search.php file in your template folder 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

This is just one of the cool things that you can do for your Search Page when customizing it.

Source: Michael Martin

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

Comments

5 Responses to “Display Search Term and Result Count in WordPress”
  1. 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.

  2. 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); ?>

  3. 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

  4. pete says:

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

Share Your Opinions

Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!

Please make sure that you have read our Comment Policy.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar