WordPress Wednesdays: Improving your search results page

In this WW post, we will explain some tricks and tips to make your WordPress search results friendlier, and more helpful for your users.

There are a number of plugins that have built boasting their improvements to WordPress’s out-of-the-box search, but some simple tweaks can be done to refine your results, and improve the search result’s page interface.

The below examples involve editing your template files, so make sure to back everything up before making any changes!

Display the search term on the results page

On the search page, a simple UI tweak can be adding the search term to either the page’s title, or an introductory paragraph.

<p>Your search for <?php the_search_query(); ?> has returned the following results:</p>

Alternatively, when there are no results, you can display:

<p>Your search for <?php the_search_query(); ?> has returned no results. Please double check your spelling, or try a new search below:</p>

Add a <span> around the term with a background color, or emphasize the term in some other way when you output the code to make it really stand out:

Include a search box on the results page, whether results are returned or not

It’s common to display a search box when a search has returned no results, but it can also be handy to display the search at the bottom of each results page, in case the returned results are not what the user was looking for.

Include the below code after the <?php endwhile; ?> portion of the loop, but before the <?php endif; ?>:

<p>Not finding what you were looking for? Try a new search below!</p>

<form class="clearfix" method="get" id="searchcontent" action="<?php bloginfo('home'); ?>/">
<input type="text" name="s" id="s" />
<button type="submit">Search</button>

Display how many search results have been returned

WordPress returns pages of search results with no real indication of how many the user will be paging through (and how useful their search term was). You can display the number of search results that were returned at the top of the page by using the below code:

global $wp_query;
$totalresults = $wp_query->found_posts;

<p>Your search has returned <?php echo $totalresults; ?> results.</p>

Combine with the search term to indicate to users how useful their search was:

Exclude categories, pages, etc. from the search results

Oftentimes, there can be categories, pages or posts that you don’t want included in your site’s search results. You can add a search filter to your functions.php file that will exclude specific categories:

function filterCategory($query) {
if ($query->is_search) {
return $query;

Alternatively, you can add a similar filter that will exclude an entire post type:

function filterPostType($query) {
if($query->is_search) {
$query->set('post_type', 'post');
return $query;

The filters will automatically be added to all search results that are returned on the site using the WordPress search.

Alternatively, you can exclude pages, posts, categories etc. on the search results page itself — the below code added before the loop on the search results page will restrict the returned result to just posts, rather than pages and posts — just include the below code on your search.php page before the loop:

global $wp_query;
$args = array_merge($wp_query->query, array( 'post_type' => 'post' ));
query_posts( $args );

Do you use tweaks and plugins to alter your WordPress search results? Please share them in the comments below!

Comments are closed.

Legal FAQ Collections