How to: Display the number of results in WordPress search

March 18, 2009 at 10:27 am

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!