Display the Total Number of Search Results  Found

WordPress Search Template - Total Number of ResultsIt’s a common design pattern for search engines to display the total number of search results found. Surprisingly most WordPress themes don’t do this, and it’s incredibly easy. There are a few methods out there, but I’ve found that this is the most efficient and effective way.

Add this to your functions.php file

/**
*	A title for the search.php template that displays the total number of search results and search terms 
*/
function search_results_title() {
	if( is_search() ) {
	
		global $wp_query;
		
		if( $wp_query->post_count == 1 ) {
			$result_title .= '1 search result found';
		} else {
			$result_title .= $wp_query->found_posts . ' search results found';
		}
		
		$result_title .= " for “" . wp_specialchars($wp_query->query_vars['s'], 1) . "”";
		
		echo $result_title;
	
	}
}

This function will output allow you to use a new template tag in your theme called search_results_title(). The template will output the following HTML:

HTML Output

4 search results found for “lorem”

Add the title to your search.php template file

Replace the main title (usually an H1) on your search.php template with the following bit of code:

 if( function_exists('search_results_title') ): ?>

Search Results Found

That's it, you now have a better user experience on your WordPress search results page! If you'd like to take your WordPress site search to the next level I suggest checking out Yoast's helpful post on making the WordPress search suck less.

Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 17 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.