Make WordPress' search function suck Less.

WordPress default search kinda sucks. It sorts the results by date, newest first, and interface wise it doesn't have any of the cool things we're used to when we search in for instance Google.

This post will explain how you can make your search experience suck less. It requires a few plugins and some work on your search.php template file, but it will make you and your users happier in the end.

Sort by relevance, not date

It has already been noted several times, and it's mentioned in the WordPress Trac at least once: it'd be nice of WordPress would sort results by relevance instead of date. Luckily, you and me are not the only one to think that. The people at Semiologic have created "Search Reloaded", a plugin that will bypass the default search and use a more meaningful query to search the database.

It's not perfect, but now, search sucks less. Before installing this plugin, searching for "WordPress" on this site would return 10 recent posts, instead of my WordPress plugins page, which should be number one. Now it is.

Improve the search result interface

First of all, WordPress shows "default" excerpts. It doesn't bold the search terms you used, as search engines do these days, and it doesn't find a snippet that actually matches your query, it just shows the excerpt, or the first paragraph of the post.

This can be fixed by installing Scott Yang's Search Excerpt plugin. This plugin replaces the default excerpt with an excerpt that actually matches your query, and automatically bolds the words you sought for.

Now to make the interface into a complete search fiesta, we have to bold the keywords you search for in the titles as well. This would be a cool addition to the Search Excerpt plugin, but for now, we'll hack it into the search.php template file.

Open up search.php, and find where it says the_title();. Replace that with echo $title;. Before this line, add the following:

<?php
	$title 	= get_the_title();
	$keys= explode(" ",$s);
	$title 	= preg_replace('/('.implode('|', $keys) .')/iu',
		'<strong class="search-excerpt">\0</strong>',
		$title);
?>

It wraps the highlighted keywords in the same strong tag as the Search Excerpt plugin uses, so you can style these bolded keywords. You might give them a yellow background for instance, by adding this to your theme's stylesheet:

strong.search-excerpt { background: yellow; }

Another nice touch is changing the default title, so it reflects the query used, change:

<h2>Search Results</h2>

Into:

<h2>Search Results for "<em><?php the_search_query() ?></em>"</h2>

Pagination

Now by default WordPress will show 10 results on a page, and because of that, so will Search Reloaded. On my blog the link to the next 10 results didn't work though, and I actually wanted more search results on one page, so I opened up sem-search-reloaded.php and on line 102, I changed this:

$posts_per_page = $wp_query->get('posts_per_page');

Into this:

$posts_per_page = 25;

And now it will show 25 results. Because scrolling down 25 results is a bit of work, if people haven't found anything, you want to give them the option of easily refining their search. So below the results, after the endwhile; in your search.php, you add another search box, something like this:

<h3>Didn't find what you were looking for? Refine your search!</h3>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>

Even better would be to show people related searches, which would help them refine their search. To do this, install my Search Suggest plugin, and add

<?php related_searches(); ?>

Below the <h3> you added above. This will show a small listing of related searches, using the Yahoo! API.

Catching typo's

Another thing users have come to expect from the big search engines is that they will "catch" it when you have a typo in your query. If the user entered a query which doesn't give any results, you can use the Search Suggest plugin as well.

This time we'll use the other function in this plugin to give a spelling suggestion. Below the "no posts found" line, add the following:

<?php spell_suggest(); ?>

Now, if no posts were found and the Yahoo! API can come up with a better way of spelling the query, your user will get a nice "Did you mean x?" reply, where x is a clickable link.

Tracking search

There are several ways of keeping track of what people are searching for on your blog. One is to use Search Meter, a plugin that creates a panel on your dashboard that keeps track of searches, and also shows which searches didn't give any results. When testing it on WordPress 2.6 I found it wasn't saving all the searches that were done on my blog.

Another way, which I prefer, is to use Google Analytics and track site search in there. It's easily set up:

  1. Go into your Google Analytics account
  2. Click "edit" next to the correct profile in your Analytics settings
  3. Click "edit" again in the top right of the first info block
  4. You'll get a screen called "Edit profile information"
  5. Under "Site Search" click "Do track site search"
  6. In the text input field under "Query Parameter", enter "s" (without the quotes)
  7. Click Save Changes
  8. You're done!

Once you've done all this, you should be able to continually improve on your user's search experience! So, tell me, what is your way of improving the WordPress search experience?

Enjoyed this article?

Subscribe now with RSS, daily or weekly emails to receive more tips, tricks and ideas on improving your website!

42 Comments to “Make WordPress' search function suck Less.”

  1. Cormac Moylan

    Cormac Moylan August 4th, 2008 at 22:07

    Epic post, Joost. I have an improvement in mind for Wordpress search but I am going to try to test it out myself first to see if it works. :)

  2. adam

    adam August 4th, 2008 at 23:49

    Great Idea,s but please edit your post: echoing the search result is begging to be exploited. please use <?php the_search_query() ?>, which will automatically escape dangerous characters in the search query.

  3. Joost de Valk

    Joost de Valk August 5th, 2008 at 06:47

    @Adam you're absolutely right! Fixed!

  4. André Scholten

    André Scholten August 5th, 2008 at 07:47

    Nice suggestions, implemented it rightaway. I never noticed the search results where date-sorted in stead of relevancy sorted.

  5. Roel Willems

    Roel Willems August 5th, 2008 at 10:47

    Great post Joost,

    I would recommend also echoing the title (bold when matching the search query) within the read more link after the excerpt. Like in the following example:

    <a href="" rel="bookmark" title="">Read more about

  6. Roel Willems

    Roel Willems August 5th, 2008 at 10:49

    (forgot the tags..., my bad)

    <a href="" rel="bookmark" title="">Read more about

  7. Shanker Bakshi

    Shanker Bakshi August 5th, 2008 at 10:50

    accept my heartiest congratulation on being appointed by Shoemoney in his TEAM for Shoemoney Tools. I am sure you will rock the blogsphere. You time has just begin. Make your server strong enough to handle the traffic.

  8. David Bradley

    David Bradley August 5th, 2008 at 12:32

    Never occurred to me to use the standard WP search box, I've always had a google search which is monetisable, makes a few bucks a week and reports back on all searched terms, which can be useful. TGN1412 remains the best term on sciencebase.com for some bizarre reason...

  9. Big Blogger

    Big Blogger August 5th, 2008 at 12:41

    Thanks Joost, for making available this cool informations.

    ciao
    alex

  10. Phil v. Sassen

    Phil v. Sassen August 5th, 2008 at 13:04

    Great article! Thanks.

    Cheers, Phil

  11. Shanker Bakshi

    Shanker Bakshi August 5th, 2008 at 14:47

    I am not using WP search widget . The reason being

    1. Its search box bar is too short
    2. Its shows full post as a search result - Need only title and related short text

    Hey Joost If you can guide me how to fix it - i will re-install it.

  12. Jonathan

    Jonathan August 5th, 2008 at 15:51

    Fine!!! i was just wondering about how to keep an eye at Wordpress Searchs, this article is faaaar more relevanto to the question. Thanks!!!

  13. Tony

    Tony August 5th, 2008 at 16:47

    Thanks for the great tips! I did have problems with the related search plugin looking for snoopy, i had to require_once(ABSPATH . 'wp-includes/class-snoopy.php'); in the plugin for it to no error out. still didnt return any results, but maybe it's an api key issue...

  14. Freelance Web Design

    Freelance Web Design August 5th, 2008 at 20:17

    Thanks for this. I implemented on two of my blogs. Very cool.

  15. Mikael Rieck

    Mikael Rieck August 5th, 2008 at 22:42

    I'm constantly amazed with the great ideas you come up with Joost. Not only do you create some killer plugins but you’re also filling out some obvious holes when it comes to making the perfect wordpress blog. Please don’t ever stop doing what you’re doing.

    /Mikael

  16. Joost de Valk

    Joost de Valk August 5th, 2008 at 22:44

    I'll try not to Mikael :) I love doing it :)

    Thanks for all the great feedback people!

  17. Mark

    Mark August 6th, 2008 at 04:00

    Absolutely brilliant thanks for the information.

  18. Dexter | Tech At Hand Dot Net

    Dexter | Tech At Hand Dot Net August 6th, 2008 at 06:37

    Why I can't find this one ?

    $posts_per_page = $wp_query->get('posts_per_page');

    I am using revolution

  19. Joost de Valk

    Joost de Valk August 6th, 2008 at 06:47

    @Dexter it should be in the plugin, not your theme.

  20. Dexter | Tech At Hand Dot Net

  21. Teufel

    Teufel August 9th, 2008 at 04:17

    Great post! This helped me out a lot with setting up the search functionality for my site!

    I did try the "Search Reloaded" plugin and it works okay, but for some reason it ignores the next/previous entries links and is kind of limited. I was able to track down a much more feature-rich plugin Advanced Search which has almost every function you could ask for including; searching a word or a string, whether to include (or exclude) searches in posts, pages and comments, sort by relevance or date and whether it displays in ascending or descending order. It's a bit of a pain to setup, since all editting is done manually and you can't change the form through the admin area (it doesn't even install a page in the admin area), but after adding the snippit of code mentioned under the heading "… if you want a sidebar search form:" you're able to see the search bar on your page. I just manually editted it and setup a bunch of hidden inputs to make things display the way I want. It's definitely worth the effort. Plus it has the keyword highlighting function built-in to the search.

    Check it out, I think it's way more functional than "Search Reloaded", a bit of a pain in the ass to use, but it works great.

  22. SEO/SEM blog

    SEO/SEM blog August 9th, 2008 at 15:25

    I have been using search unleashed before and when i switched to your tips and plugins its much better. The only thing i miss is the log file where i could see detais of each search, ip address and so on. I have set up Analytics according to what you wrote but it doesnt show anything. Well i will test it for more days and see what happens.

  23. DIYGuy

    DIYGuy August 9th, 2008 at 23:38

    Very much appreciates! I've always hated the way WP displays searches and this makes it much better, I even liked the fact it only took about 15 minutes to integrate.

    Thanks!

  24. Blog Step Wise

    Blog Step Wise August 12th, 2008 at 19:19

    Nice post, very valuable and informative.

  25. Jean-Paul Horn

    Jean-Paul Horn August 13th, 2008 at 06:53

    I'm afraid I'm seeing (or rather: not seeing) the same problem as Tony. I can't get spell_suggest or related_searches to output anything. I'm using my own Yahoo API key and I made sure the full Yahoo url with API and querystring works as expected, but your plugin doesn't show anything :-( It does on your own blog though. What's the difference?

  26. Robert Augustin

    Robert Augustin August 16th, 2008 at 02:32

    Joost,
    This is great. Wonderful. Breathtaking! I subscribed immediately after reading this post.

    After such kudos, maybe you can tell me why I get a PHP error whenever I try to call either spell_suggest(); or related_searches();. Both times, I get something like this (WP 2.6):

    Fatal error: Cannot instantiate non-existent class: snoopy in /wp-content/plugins/search_suggest.php on line 35

    Other than that, I love your blog and will remain subscribed :)

    Keep it up!

  27. Joost de Valk

    Joost de Valk August 16th, 2008 at 06:58

    @Robert Augustin and others: try to download it again, I've just found what might be causing the error, fixed and put a new zip file on the page.

  28. archshrk

    archshrk August 17th, 2008 at 05:59

    This is a lot harder to implement when you don't have a search.php in your theme.

  29. Joost de Valk

    Joost de Valk August 17th, 2008 at 07:52

    then make one :-)

  30. Nicolas

    Nicolas August 17th, 2008 at 15:12

    Hi Joost,
    I tried your Search Suggest plugin, and I seem to have found in the "related_searches();" function. Indeed, when Yahoo! API only returns one result, it puts it in a string, not an array ; causing the "related_searches();" function to fail with:

    "Warning: Invalid argument supplied for foreach() in [...]search_suggest.php on line 47"

    I have fixed the function with the following

    if (isset($resultset['ResultSet']) && $resultset['ResultSet'] != "\n") {
    if ($full) {
    if (is_string($resultset['ResultSet']['Result'])) {
    $result = $resultset['ResultSet']['Result'];
    $output = "$result";
    } else {
    foreach ($resultset['ResultSet']['Result'] as $result) {
    if ($output != "") {
    $output .= ", ";
    }
    $output .= "$result";
    }
    }
    $output = "You might try these searches as well:".$output."";
    echo $output;
    } else {
    foreach ($resultset['ResultSet']['Result'] as $result) {
    $output[] = $result;
    }
    return $output;
    }
    } else {
    return false;
    }

    Hope it helps,
    Nicolas

  31. Joost de Valk

    Joost de Valk August 17th, 2008 at 18:11

    @Nicholas: Thx for the fix! I'm combining them with some other fixes and will release a new version in a couple of minutes.

  32. Joost de Valk

    Joost de Valk August 17th, 2008 at 18:29

    Ok I've just released version 1.1, find it at the Search Suggest page. The plugin is now also officially hosted at wordpress.org, find it here, I'd appreciate some good ratings :)

  33. semantik

    semantik August 20th, 2008 at 16:44

    thanx for the advice yoast... have already implemented a substantial number of your suggestions.

  34. archshrk

    archshrk August 20th, 2008 at 20:46

    "then make one :-) "
    Oh, I did. In fact, I've made three four so far. i just didn't realize I didn't have them until I tried implementing your tips. Thanks for this great article.

  35. adam

    adam August 20th, 2008 at 21:05

    please unsubscribe me from the comments for this post. your subscribe to comments plugin is broken.

  36. Robert Augustin

    Robert Augustin August 21st, 2008 at 17:53

    Joost,

    OK I got around to try the new version of search suggest and Snoopy doesn't cause any more problems now :) Works just fine., thank you!

    On another note, would you recommend using my own/another App ID? Or is it intended to run on yours - I don't know what kind of limits there are on those.

    Thanks again Joost.

  37. Morten

    Morten August 22nd, 2008 at 11:00

    Joost,

    If i dont have any search.php or query.search.php an i have to make one as you suggested to, where do i put it, in the root folder?

  38. Jean-Paul Horn

    Jean-Paul Horn August 23rd, 2008 at 05:10

    Search.php belongs in your theme folder. Just copy the one from the default (Kubrick) theme and modify per the instructions.

  39. AnneTanne

    AnneTanne August 25th, 2008 at 08:29

    I though your article would definitely help me out...
    I use WordPress as a CMS, and although I have a frequently updated blog, most people seem to visit my site for it's static content. I was (I am) using the 'Search pages' plugin, because the default WP-search only searches the posts, not the pages. But this still isn't sufficient, because the results aren't relevance-sorted.
    While reading your post, it seemed to me that the Search Reloaded plugin also searches pages, but no... after installing the plugin and deactivating (or not) the Search Pages plugin, no pages showed up in the search results, which make it useless for my site.

  40. Joost de Valk

    Joost de Valk August 25th, 2008 at 09:31

    @annetanne: upgrade :) wp 2.5 and up search pages by default.

  41. AnneTanne

    AnneTanne August 25th, 2008 at 10:16

    I don't have the impression that in my 2.6 install pages are searched???
    But I'll have a look at that...

  42. AnneTanne

    AnneTanne August 25th, 2008 at 10:43

    OK, you're right... My pages are being searched...
    But I'm afraid there some problem with my theme or install, because neither 'Search reloaded' nor 'Search exerpt' seem to work. (The same search results are shown, in the same order, and, as you pointed out already, no link to the 'next results' with 'Search reloaded')
    So I think I'll stick with the basic WordPress search.

Leave a Reply

Joost de Valk a.k.a. Yoast Want an avatar too?
Go to gravatar.com and upload your preferred avatar.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

9 Trackbacks and Pingbacks

  1. WooThemes
  2. SEO Hosting Blog - Improve WordPress Search
  3. Lee.org » Installed Search Excerpt and Search Reloaded
  4. Link Post Sunday 08/10 | Mr Sun Studios
  5. 11 tips för en bättre blogg | Webmastern.se
  6. 24 Terrific Tweets
  7. 40+ Metric F***Tons Of Awesome Resources
  8. eXtra For Every Publisher » Blog Archive » Insights into Building a Site from Scratch: Round Four
  9. Kerry Webster » The Nature of WordPress Search
Hosting by:
Hosted by MediaTemple Grid Services