Don’t Let Your Empty Search Page Drive Off Visitors

Don’t Let Your Empty Search Page Drive Off Visitors

Keeping visitors on your site is vitally important, so the last thing you want for those searching your site is a page with little more than the message “no results found”.

That’s telling visitors to “move along, there’s nothing of interest to see here”.

But you can change this message by making your search results page work much harder. Here’s how.

Screenshot of no results search page asking visitor to leave immediately
This is probably how your no results search page reads.

A recent Alertbox from UX gurus, Nielsen Norman Group, talked about search results and the consequences of having poorly designed or even empty search results pages.

Whilst empty search results on the the major search engines are a rare occurrence, they are far more likely on the average WordPress site so are a potential huge turn-off for your visitors.

The Alertbox made three recommendations for search results pages:

  1. Make it obvious that there no results
  2. Offer starting point for moving forward
  3. Don’t mock the user

The first recommendation is really about the clarity of the page and making the message stand out.

The last recommendation is targeted at those sites that attempt to use humor when no results are found. In these cases, NNG says to be careful in case the visitor thinks you are poking fun at them.

We are going to focus on the second recommendation which NNG broke down to the following:

  1. Restate the query, so the visitor knows what they searched for
  2. Provide a search box pre-filled with the original query
  3. Suggest similar queries that do return results
  4. Suggest spelling corrections (as per Google’s “did you mean” functionality)
  5. Provide advice about modifying queries

To meet as many of the above recommendations as possible and to try and keep visitors on our site, we are going to build a new search page to replace your theme’s current search page.

Overriding The Existing Template

Each theme will have its own approach to displaying search result pages. TwentyFourteen, which I’ll be using in this article, has a search.php template that uses the generic templates content.php and content-none.php to format the pages, depending on whether there are any results or not.

Regardless, we’ll create our own but you’ll need to ensure that it keeps to the style your theme uses. For this reason, we’ll be doing all the modifications in the theme rather than via a plugin.

So, first job is to add this small function to your theme’s functions.php file:

This code hooks into the template_redirect action that gets called when WordPress is deciding which template to use to create the page.

We’ve got a check in there to make sure it’s the search page that’s being requested and if it is then we are going to use our own template, searchpage.php.

Creating The New Search Template

In your theme directory create a new file called searchpage.php.

As I mentioned before, you need to ensure that you maintain your site’s look and feel. If you are just playing along on TwentyFourteen, then you’ll be fine.

If not, then one option is to copy the contents of a template such as page.php and then replace the dynamic content – generally where the Loop starts – with everything below contained in the #content div.

Okay, so let’s walk through this code and see what it does.

Restating The Query

The get_search_form function from WordPress outputs the search form and automatically pre-fills the form with the search term.

Displaying Results

Screenshot of simplified search results
Visitors expect search results to look something like Google.

I’ve simplified the display of any results. NNG points out that virtually all visitors have a mental model of what a search results page looks like: it looks a lot like Google.

By using the content.php template, TwentyFourteen was displaying a lot of extra information, including metadata and comment counts, so I’ve stripped it down to just title and excerpt (another reason why explicit excerpts are so important).

When There’s No Matches

So, the major differences are to be seen when there are no search results to display. TwentyFourteen’s default response is to display the search form with the message that there are no search results which means that it neglects most of the NNG recommendations.

Adding Did You Mean? Functionality

We are all familiar with Google correcting our mistyped search queries or suggesting alternatives.

Whilst we are not going to do the former, we can quite easily do the later by utilising the Wikipedia API. I chose to implement this using client-side code as I don’t want it to hold up the display of the page if the API was slow in responding or not available.

You’ll notice that there is a placeholder #didyoumean div: this is where any suggestion message will be displayed.

At the bottom of the template, before the list_posts helper function, there are two script tags. The second is a direct call to the Wikipedia API with the search term being inserted using the get_search_query WordPress function.

This creates a JSON formatted payload of suggested alternatives, if there are any, that is passed to the didyoumean function courtesy of the callback=didyoumean querystring parameter on the API call. This also allows us to get around cross-domain scripting errors.

The didyoumean function simply checks the returned data and if there is a suggestion an appropriate message is inserted into the placeholder.

Providing Advice About Modifying Queries

This is simply including content in the page. I’ve made it pretty short and sweet, so alter it to suit your needs.

Suggesting Similar Queries That Return Results

Now, this is very sophisticated functionality and overkill for the majority of sites; but providing suggestions for alternative content strikes me as being important, so I’ve gone for a more lateral solution and listed the latest five posts and the five posts with the most comments.

This is achieved by setting up the arguments for calling WP_Query and then calling the list_posts function to run the query and output any results.

You can change this to whatever list of posts you think is appropriate. If you use Jetpack Stats on your site, you could use the Jetpack Popular Posts plugin to list the most popular posts by page visits. There are also other plugins that will count visits and provide a list of popular posts.

Obviously, these lists will work better the longer you’ve been collecting statistics.

The End Result

So, here’s the end result of our new search template.

Screenshot of improved no results search page with suggestions and helpful tips
At least we are now trying to help the visitor stay on the site a bit longer

It’s certainly not as empty looking as its predecessor and, hopefully, with plenty of links available won’t immediately be moving your visitors on.

UPDATE: Automatically Performing The Suggestion Search

If, like Paul (see comment below), you’d like to have the search results for a suggested term automatically displayed then add this code to your functions.php:

This adds a javascript function to the page that makes an ajax call to WordPress to execute the suggsearch function.

You also need to make a small change to searchpage.php to replace

{code}
if (sugg) {
jQuery(‘#didyoumean’).html(‘<p>Did you mean <a href=”?s=’ + sugg + ‘”>’ + sugg + ‘?</a></p>’);
}
{/code}

with

{code}
if (sugg) {
suggestion_search( sugg );
}
{/code}

The new javascript function now takes care of outputting the “Did you mean” message. It will only do so if search results are found for the suggestion as there’s not much point in suggesting an empty search.

Do you have a site search? Do you use any of the above techniques to help your visitors if their search returns no results?