Post Pic

How to: Display the number of results in WordPress search

WordPress users know it: Search is definitely one of WordPress weak point. Today, let’s see how to get the number of results the search returned, and display it (proudly!) on our blog.

Open your search template file, search.php. In it, search for the following:

<h2 class="pagetitle">Search Results</h2>

Now replace this with:

<h2 class="pagetitle">Search Results 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(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>

That's all, you're done!

Credits goes to ProBlogDesign for this awesome WordPress recipe!

By the way, I'm going to publish a big article of 10 awesome WordPress hacks on my other blog Cats Who Code tomorrow. Be sure to suscribe to our RSS feed so you're gonna miss it!

2 Responses

Oct 02 2011 05:50

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:

http://codex.wordpress.org/Creating_a_Search_Page#Display_Total_Results

Dec 29 2011 01:23

I love your blog!

I’m a WordPress developer working for a graphic design firm, and ever since I first started using WP, your articles have always had just what I need to add that little fancy touch to one of my websites.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required