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?

Tags:
Category: WordPress
You can skip to the end and leave a response.

119 Responses to “Make WordPress’ search function suck Less.

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. :)

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.

@Adam you're absolutely right! Fixed!

Pingback: WooThemes

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

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

(forgot the tags..., my bad)

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

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.

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...

Thanks Joost, for making available this cool informations.

ciao
alex

Great article! Thanks.

Cheers, Phil

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.

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

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...

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

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

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

Thanks for all the great feedback people!

Absolutely brilliant thanks for the information.

Why I can't find this one ?

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

I am using revolution

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

Pingback: SEO Hosting Blog - Improve WordPress Search

@ Joost ,, Thanks for the info

Pingback: Lee.org » Installed Search Excerpt and Search Reloaded

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.

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.

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!

Pingback: Link Post Sunday 08/10 | Mr Sun Studios

Pingback: 11 tips för en bättre blogg | Webmastern.se

Pingback: 24 Terrific Tweets

Pingback: 40+ Metric F***Tons Of Awesome Resources

Nice post, very valuable and informative.

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?

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!

@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.

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

then make one :-)

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

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

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 :)

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

"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.

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

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.

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?

Pingback: eXtra For Every Publisher » Blog Archive » Insights into Building a Site from Scratch: Round Four

Pingback: Kerry Webster » The Nature of WordPress Search

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

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.

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

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

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.

Pingback: Hot Links To Beat The Heat | This Month In SEO - 8/08 | TheVanBlog | Van SEO Design

Pingback: We’ve redesigned come and take a look

Pingback: (Anti) Social-Lists 8/31/08 | (Anti) Social Development

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

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!

Pingback: Leonaut.com

Pingback: Tweaking your WordPress Website | archshrk

thanks a lot for sharing. Good luck for you

Pingback: Felhasználóbarátabb WordPress keresés : Invalid Click :: SEO Blog

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.

Pingback: absatzsetzer.de | Verbesserung der Blog-Suche

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

Pingback: 11 Top WordPress Plugins Every Blog Should Have

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

Pingback: What tools can you use to measure WHY people are leaving your site « :: Will Scully-Power :: Data, Analytics & Optimisation in the World of Advertising!

Pingback: Tools to measure WHY people are leaving your site « :: Will Scully-Power :: Data, Analytics & Optimisation in the World of Advertising!

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

Does anyone have any advice on this?

Pingback: A Better Wordpress Search | Top Blogger

Pingback: Better WordPress Search Needed

Pingback: » 14 basic plugins no Wordpress blog should be without | The Pink Crow

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.

Thanks Lars, I'll check out that plugin!

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.

Pingback: Make WordPress search more useful | Brass Blogs Web Design

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!

Pingback: Features of a blog worth caring about « Web Strategy Directory

Pingback: Most Exceptional WordPress Hacks « Online Free Applications Software Tips Tools Wallpapers

Pingback: 10 Exceptional WordPress Hacks | Bookmarks

Pingback: Wordpress-Hacks - Gestern war ich mal wieder auf Weltreise, Wartungsseite einrichten, Links zu ähnlichen Artikeln, Suchbegriffe bei den Suchergebnissen hervorheben, Wordpress-Themes - die Dateien und deren Bedeutungen, Der eigene kostenlose Blog Eine Wor

Pingback: 10 Exceptional WordPress Hacks « the gypsy

Pingback: romulus23.de » Bessere Suche für Wordpress

Pingback: 10 Cool WordPress Hacks | Knowledge Base eDynamo

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

Pingback: Super important SEO plugin for wordpress

Pingback: How To Combine Google Custom Search and AdSense with the Default Wordpress Search | Zemalf

Pingback: Die WordPress Suche - erweitern, verbessern und funktionsfähig machen « Der Webanhalter

Pingback: Make WordPress’ search function suck Less. - Yoast - Tweaking Websites | Squico

Pingback: WordPress Search Engine: Get Beautiful and Revelant | Mark8t: SEO, SEM, E-Marketing And More

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

Pingback: Is It That Important To Get Rid Of Wordpress Search ? | GotChance - A Geek's Blog

Pingback: 10 New Wordpress Hacks | Themeflash

Pingback: The Newsroom » Blog Archive » Tweaking Sites With Joost of Yoast

Pingback: How to Blog: Features of a blog worth caring about | Josh Klein Web Strategy

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

Pingback: Best free Wordpress Plugins | Premium News Theme

Pingback: 14个免费的网站访客行为分析工具:之十二Google Service | 互联网的那点事...

Pingback: 15 PHP regular expressions for web developers

Pingback: 5 Useful but Lesser Known Wordpress Plugins | Armchair Theorist

Pingback: 15 PHP regular expressions for web developers « I.T News & Stuff

Pingback: 15 PHP regular expressions for web developers « endo – explosion kiss

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?

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.

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.

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

Yes ok, fixed. Thx.

Pingback: 35 WordPress Plugins that Get the Job Done: Freelancing Favorites » DQuinn.net

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

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

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.

Pingback: Number of Site Search Results in Google Analytics

Pingback: Improving Site Search With Rules, Analytics & Rich Auto Complete

Pingback: 10 Exceptional WordPress Hacks | 9Tricks.Com - Tips - Tricks - Tutorials

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

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

Pingback: Naprednejši iskalnik za Wordpress | Pomagalnik

Pingback: Stupid WordPress Tricks | Serita

Comments closed, if you feel you have something to say:
drop me a line.