DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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!

5 Comments

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

  2. Loren Helgeson

    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.

  3. I need to know how to limit the search results on a specific post type to only about 3-5 posts – per page without pagination

  4. I thought I’d let you know that you should use $wpdb->found_posts rather than post_count as post_count is only returning the number of posts that is going to be displayed.

  5. Thanks for the heads up on $query->have_posts commenters! Much cleaner.

Leave a Comment

Your email address will not be published. Required fields are marked *