Make WordPress’ search function suck Less.

August 04th, 2008 – 119 Comments

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?


119 Responses to Make WordPress’ search function suck Less.

  1. Cormac Moylan
    By Cormac Moylan on 4 August, 2008

    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
    By adam on 4 August, 2008

    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
    By Joost de Valk on 5 August, 2008

    @Adam you’re absolutely right! Fixed!

  4. André Scholten
    By André Scholten on 5 August, 2008

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

  5. Roel Willems
    By Roel Willems on 5 August, 2008

    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
    By Roel Willems on 5 August, 2008

    (forgot the tags..., my bad)

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

  7. Shanker Bakshi
    By Shanker Bakshi on 5 August, 2008

    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
    By David Bradley on 5 August, 2008

    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
    By Big Blogger on 5 August, 2008

    Thanks Joost, for making available this cool informations.

    ciao
    alex

  10. Phil v. Sassen
    By Phil v. Sassen on 5 August, 2008

    Great article! Thanks.

    Cheers, Phil

  11. Shanker Bakshi
    By Shanker Bakshi on 5 August, 2008

    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
    By Jonathan on 5 August, 2008

    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
    By Tony on 5 August, 2008

    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
    By Freelance Web Design on 5 August, 2008

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

  15. Mikael Rieck
    By Mikael Rieck on 5 August, 2008

    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
    By Joost de Valk on 5 August, 2008

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

    Thanks for all the great feedback people!

  17. Mark
    By Mark on 6 August, 2008

    Absolutely brilliant thanks for the information.

  18. Dexter | Tech At Hand Dot Net
    By Dexter | Tech At Hand Dot Net on 6 August, 2008

    Why I can’t find this one ?

    $posts_per_page = $wp_query->get(‘posts_per_page’);

    I am using revolution

  19. Joost de Valk
    By Joost de Valk on 6 August, 2008

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

  20. Dexter | Tech At Hand Dot Net
    By Dexter | Tech At Hand Dot Net on 6 August, 2008

    @ Joost ,, Thanks for the info

  21. Teufel
    By Teufel on 9 August, 2008

    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
    By SEO/SEM blog on 9 August, 2008

    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
    By DIYGuy on 9 August, 2008

    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
    By Blog Step Wise on 12 August, 2008

    Nice post, very valuable and informative.

  25. Jean-Paul Horn
    By Jean-Paul Horn on 13 August, 2008

    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
    By Robert Augustin on 16 August, 2008

    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
    By Joost de Valk on 16 August, 2008

    @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
    By archshrk on 17 August, 2008

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

  29. Joost de Valk
    By Joost de Valk on 17 August, 2008

    then make one :-)

  30. Nicolas
    By Nicolas on 17 August, 2008

    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
    By Joost de Valk on 17 August, 2008

    @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
    By Joost de Valk on 17 August, 2008

    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
    By semantik on 20 August, 2008

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

  34. archshrk
    By archshrk on 20 August, 2008

    “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
    By adam on 20 August, 2008

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

  36. Robert Augustin
    By Robert Augustin on 21 August, 2008

    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
    By Morten on 22 August, 2008

    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
    By Jean-Paul Horn on 23 August, 2008

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

  39. AnneTanne
    By AnneTanne on 25 August, 2008

    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
    By Joost de Valk on 25 August, 2008

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

  41. AnneTanne
    By AnneTanne on 25 August, 2008

    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
    By AnneTanne on 25 August, 2008

    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.

  43. Evert Jan
    By Evert Jan on 7 September, 2008

    Really great post Joost! I’ve implemented some of your tips on my blog. How about internationalizing your Search Suggest plugin? I had to “hack” the code now to translate things to Dutch.

    Gr,EJ

  44. Jean-Paul Horn
    By Jean-Paul Horn on 7 September, 2008

    Originally Posted By Evert JanReally great post Joost! I’ve implemented some of your tips on my blog. How about internationalizing your Search Suggest plugin? I had to “hack” the code now to translate things to Dutch.

    Hi Evert Jan,

    Care to share your hacks? I searched the Yahoo API, but couldn’t find any stuff about the Dutch part of Yahoo Suggest. Would love to know how you made this work!

  45. Kudungga
    By Kudungga on 11 September, 2008

    thanks a lot for sharing. Good luck for you

  46. archshrk
    By archshrk on 24 September, 2008

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

    You may want to start with your themes archive.php or category.php if it has these.

  47. Morecambe
    By Morecambe on 12 October, 2008

    Thanks for this. Now have it on 4 of my WP sites – wicked!

  48. Larry Bradshaw
    By Larry Bradshaw on 3 November, 2008

    How would I show the # of results, preferably displayed on a line, ie. “xx results found”? prior to displaying the posts returned?

  49. Elana's Pantry
    By Elana's Pantry on 10 November, 2008

    Does anyone know if the Search Reloaded plugin is capable of searching comments as well?

  50. Elana's Pantry
    By Elana's Pantry on 14 November, 2008

    Does anyone have any advice on this?

  51. Lars
    By Lars on 14 March, 2009

    From what I could tell from people requesting comments to be included in Search Reloaded it does not seem to do this yet. Regardless the Search Reloaded plugin is no longer free to use anyway, they’re asking $300 for it.

    Another (better) alternative might be wpSearch which uses Lucene for the search. I have not had a chance to try it out yet but will definitely give it a go since it seems to be one of the few plugins available to really make a difference to the relevancy in wordpress search.

    • Joost de Valk
      By Joost de Valk on 14 March, 2009

      Thanks Lars, I’ll check out that plugin!

  52. Mike Carter
    By Mike Carter on 19 March, 2009

    I just set up my first blog, Artist’s Inlet Press, as an adjunct to my main site. As any newbee to WordPress, I’m in a plug-in frenzy. Going through the search plug-ins on wordpress.org, I somehow arrived here. This is THE most helpful and informative set of information I’ve discovered. My SEO and user database growth really begins by reading the material on these pages and employing the suggested downloads.

    I know you enjoy this work but from my brief experience I also realize how time-consuming it is. We all appreciate the effort.

  53. James Luff
    By James Luff on 6 April, 2009

    This was such an awesome article, you have some amazing content here! I loved your SEO lecture, I learnt loads from that. I can’t wait to put this new improved search into action on my site, thank you!

  54. James John Malcolm
    By James John Malcolm on 6 April, 2009

    Thanks for the tips…

    You should try search unleased it’s a free download and works quite well!

  55. Kash
    By Kash on 22 April, 2009

    Hi Joost,

    Just a quick note to ask your opinion on Sphinxsearch as an alternative to “search reloaded”. The plugin is here:

    http://wordpress.org/extend/plugins/wordpress-sphinx-plugin/

    We have tried to use it with some addtional conditional filters – when we put more than 3 filters in, the search doesnt like it :(

    Hope this helps my fellow wordpressers.

    Regards

    Kash

  56. Chandra Wijaya
    By Chandra Wijaya on 8 June, 2009

    It does really great tips.. Thanks for sharing..

  57. Mikko
    By Mikko on 9 July, 2009

    Want most of the stuff in the entry with just one plugin? Check out Relevanssi: http://wordpress.org/extend/plugins/relevanssi/

  58. Julien Photography
    By Julien Photography on 11 August, 2009

    Thanks for the tricks, very nice.

    A small tip I found: if your WordPress web adress insn’t your Blog web adress,
    just replace the tag “wpurl” by “blogurl” on search_suggest.php.

    Please, how to remove the link who appears above the post/page title on results page?

  59. Julien Photography
    By Julien Photography on 11 August, 2009

    I’m not sure but… how to match plural word with a singular search word?
    Curiously it doesn’t work each time, maybe depending of the post content…
    (eg: “portrait” doesn’t match “portraits”, but “professionnel” match “professionnels”. Try this on my site.)
    I’m using Relevanssi too.

    • Mikko
      By Mikko on 11 August, 2009

      Julien, this is a feature of Relevanssi. If it doesn’t find matches for “portrait”, it’ll activate fuzzy search and try to find hits for “*portrait” and “portrait*”. “Professionnel” matches something, so the fuzzy search doesn’t activate.

      The next version of Relevanssi will likely add some controls over the fuzzy search (like the current setting, always on, always off), I’ve been meaning to add those features for a while.

      Hopy you get this, I would’ve emailed you directly but the contact page on your site is broken, it seems.

      • Julien Photography
        By Julien Photography on 11 August, 2009

        Thanks Mikko…
        I don’t know why, you have been directed to a draft contact page…

  60. Justin Souter
    By Justin Souter on 24 August, 2009

    Joost – great resources here, many thanks!

    btw, this plugin is 3 years old. We’re now on WP 2.8 etc. and I was wondering whether this p-i will still be ok (it’s not in the WP.org plugins database etc….)

    Would love your feedback & thanks again

    Justin

  61. MaorB
    By MaorB on 25 August, 2009

    Great tips, Joost.
    I’d wonder if you have a good solution for searching also in custom fields.
    I built a site with a some templates using custom fields, that must be included in search results.
    For the moment I’m using Search unleashed but not fully satisfied and looking for other custom fields search solutions.
    Thanks,
    Maor

  62. Miren
    By Miren on 31 August, 2009

    Great tips, however the Search Excerpt plugin is breaking my WordPress, which gives blank pages when activated. Shame the plugin is not updated, it works great otherwise.

  63. Geoff Snyder
    By Geoff Snyder on 12 October, 2009

    Hey Joost, I just wanted to take a minute and thank you for your help getting these little nuggets of useful information out there. I just started my own blog a few days ago and I was referred to you by a friend on mine. I look forward to everything you do with blogging and SEO. Thanks again. Geoff Snyder

  64. Derek
    By Derek on 29 October, 2009

    Thanks! Is there any way to make this work on WordPress MU so that it searches all the sites better?


Check out our must read articles about Analytics