How to Create a Better WordPress Search

Posted on 15. Oct, 2010 by .

WordPress’ search by default totally sucks. It doesn’t do a good job of searching your blog at all. It always has been an issue and I guess it always will be. Or maybe not.

I wanted to build a better search for my blog so I started to search for resources that would help me do that. Along the way I tested several different plugins and options. Let me show you how I created a better search for WordPress.

The best way I have found to create a better WordPress search is simply by using a plugin. I have used Search Everything for a long time but have recently realized that it’s not useful at all. All it really does is highlight the term you are searching for within the results page. But it doesn’t really create a better search page.

While trying to find options on how to create better searches I came across an old article from Joost de Valk about making a better WordPress search. The article itself was extremely helpful and I am going to follow and mention lots of the points he makes there. What he uses on his blog supposedly is the Search Reloaded plugin. When I tried to download the plugin it made me log in as a member. Thinking that I could easily sign up and get the plugin I indeed signed up. Still I couldn’t download the plugin because it seems to be part of a product the site is selling.

WordPress Default Search Problem

The huge problem that WordPress’ default search has is that it lists articles based on date and not on relevance. That means it lists the most recent articles that include the term you are searching for. So even when an article only mentions that keyword once will WordPress go back and show it in the search results.

In this article we want to create a search that shows relevant results.

More Relevant Search Results

That’s why I started to search for alternatives and came upon Search Unleashed. I did install Search Unleashed once before but I didn’t know how to use it right because by default it made the search look terrible.

The great thing about the plugin is that it indeed returns better search results as long as you know how to use it. If you rather want to rely on a premium plugin to deliver better results then take a look at the Relevant Search plugin which is the one that’s used on all of the Tut+ websites. It’s supposed to be really good but it’s not free.

I drifted off there, let’s go back to our plugin setup. Basically Search Unleashed let’s you create your own search engine by giving you the option of deciding on priorities.
That means that if you think in order to get relevant results the keyword should be contained in the page title then you should assign this page title the highest priority, in my case I chose 1. You then go ahead and click on the things that you think are relevant too, such as category, content, page excerpt, tags etc. and decide on a priority.

My priority settings for Search Unleashed, click to enlarge the image.

You can actually see how I have configured my search and the priorities I assigned to each type. For me this combination works best but you can change it however you want too. For example if you heavily use tags then this might be a good option to include. Once you have set the priorities be sure to save the settings.

Before any search results will be shown through your WordPress search you need to go to the Search Unleashed page (Tools -> Search Unleashed) and re-index your site. This might take a few moments. Once this is done search your blog using the new search.

Before you use it I recommend to change a few settings in order to make the search look nicer because for some reason the default Search Unleashed layout makes the search results look extremely ugly.
Let’s just fix this. Go to the Search Unleashed -> Options page and change the Search Engine to MySQL Fulltext. If you don’t change this you will still be stuck with the default WordPress engine. In order to also get rid of the ugly search excerpts uncheck both of these boxes:

  • Change page title on search results to reflect the search condition
  • Highlight searches on search page or default to the_excerpt

The last thing I would recommend is to scroll further down the page and to change the color of Highlight Color #1 to #FEFFBF because that way it’s not as bright.

WordPress Search Excerpts

The search results are still not looking the way they should look. To change this we are going to install the Search Excerpt plugin. Once installed it will show nicer excerpts automatically.

Style the Search Results

We now want to highlight the keyword within the heading of each search excerpt in order to make it stand out. To do that edit your search.php file (Appearance -> Editor -> search.php). Find the code which says the_title(); and change it to echo $title;. Then above it include the following code:

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

Now we need to add some CSS styling to the heading (Appearance -> Editor -> style.css):

strong.search-excerpt { background: #FEFFBF; }

Simply add it to your style.css file and save it. Another step is to show the visitors also what they are searching for. This seems dumb but is actually very helpful.

Let’s go ahead and find this code

<h2>Search Results</h2>

and modify it to:

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

That just makes the search page look nicer. Do the same thing with the “no posts found” line below the endwhile; code.

Show the Number of Results

Just before publishing this article I stumbled across an article from Michael on creating better search pages. In it he mentions to also show the number of results which is what we are going to add to the search page. Just add this code before the one above.

<?php $allsearch = &new WP_Query("s=$s&showposts=-1"); 
 $count = $allsearch->post_count;
wp_reset_query(); ?>

Then change the headline code above to:

<h2>Search results "<em><?php the_search_query(); ?></em>" -
<?php echo $count; ?> Articles</h2>

Now your visitors will also see how many relevant pages were found.

Refining the Search Result Page

To refine searches Joost de Valk suggests to put another search box below your excerpts which is a great idea. Just add this code after the endwhile; in the search.php file:

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

Once you have done that you could also add a number of related searches in order to make it easier for the visitor to find what he is looking for. In order to do this you need to have the Search Suggest plugin installed. Simply add this code below the one I mentioned above:

<?php related_searches(); ?>

This will add related searches below the search form.

Change the Pagination

Another step I suggest to do is to change the pagination links (which are usually after the endwhile; in the search.php file) that are usually below the search results. My normal pagination links are Older Entries and Previous Entries which I changed to Previous Page and Next Page to make the page look nicer.

Change the Search Form Value

One additional step I recommend to do is to change the default search form value. Google, for example, always keeps the keyword you have searched for in their search box and that’s what we are going to do too. Edit the searchform.php file and find the value=” code within the HTML <input> tag. Simply change it to:

value="<?php the_search_query(); ?>"

Now whenever you search for something the keyword will be put in the box.

Catch Misspelled Words

One very effective aspect Joost mentioned in his article is a way of catching typos. Search Suggest also suggests keywords when someone misspelled something. In order to make use of this add the following line below the “no matches” or “no posts found” code:

<?php spell_suggest(); ?>

Track Search Behaviour

In order to also track what is happening on your search page you can do one of several things. Google Analytics offers an easy solution to tracking site search (read it here) and that’s the method I prefer. If you do so then you will be able to find the search reports under Content -> Site Search in your Analytics dashboard.

Search meter is a WordPress plugin you can install which will show you what people are searching for on your blog and how many of those searches were unsuccessful. If you are more comfortable with that I would recommend to stick to that.

If you want to see the results of this post then simply search my blog using the search in the sidebar.

What do you think? Have you already tried to change the default WordPress search?

29 Responses to “How to Create a Better WordPress Search”

  1. Peter J

    16. Oct, 2010

    I’m tempted to contact WordPress developers and get them to change it so that search results are based on relevancy and not dates. Makes sense why people started using google custom search.

    Reply to this comment
    • Julius

      17. Oct, 2010

      Hey Peter,

      I don’t know why WordPress doesn’t change the search results. I personally don’t get it and there also have been a lot of complaints over the years, yet it was never changed. If you follow this tutorial then you will have a nice search for your blog and you don’t need to use Google’s custom search.

      Reply to this comment
  2. Alex

    16. Oct, 2010

    Holy crap man!
    Julius – you have hit this one out of the park.
    I have always noticed the default search on my blog sucks arse, in fact I usually use Google to find something on my own site LOL.
    Bookmarking this one and adding it to my to do list.

    Thanks for taking the time to put this together

    Reply to this comment
    • Julius

      17. Oct, 2010

      Hey Alex,

      I am glad you enjoyed the post. It indeed took some time to put all of the information together. Still it’s great to see that people find it useful.

      Once you have implemented it, be sure to tell me whether it worked out for.

      Reply to this comment
  3. Dan Lew

    16. Oct, 2010

    Hey Julius,

    This plugin seems really useful particularly if you have heaps of posts, its always a battle to search through them all!

    Reply to this comment
    • Julius

      17. Oct, 2010

      Hey Dan,

      yeah it is a useful plugin but only if you configure it right. When I first used it didn’t change any of the WordPress search results, thus it also made them look ugly. But once you have set the right options it works like charm.

      Reply to this comment
  4. Steve

    16. Oct, 2010

    LOL,
    Like Alex I am ashamed to admit I do my searching on Google for my own articles to link. This seems like a really sweet tool. I love the idea of weighting and ranking.

    Thanks heaps for giving a wonder explanation of what looks to be a kick ass tool!!

    Thanks heaps!

    Have a wonderful weekend!!

    Reply to this comment
    • Hesham

      16. Oct, 2010

      Awesome!

      I must take a look into this plugin, I knew that WordPress default search sucks, this is why I am using Google search on my blog which bring more relevant and accurate results from the indexed pages!

      Thank you Julius , this is a great post!

      Reply to this comment
      • Julius

        17. Oct, 2010

        Hey Hesham,

        yeah be sure to check it out. I installed it on my blog and with the minor changes it really works like charm. The great thing is also that you can basically create your own search engine with it.
        Google search is fine but I think you can get even better results with this plugin installed. Thanks for the comment buddy :)

        Reply to this comment
    • Julius

      17. Oct, 2010

      Hey Steve,

      I totally understand what you mean. I always found it hard to find relevant articles from my own blog. But if you really go through the whole tutorial you will be able to find your own articles a lot easier and faster.

      I am glad you enjoyed it and thanks for stopping by. I hope you will soon don’t have to use Google anymore :)

      Reply to this comment
  5. Julius – I always hated the way default WP search acted, but it never occurred to me that I could actually do something about it.

    Well, now I know. :)

    It looks like a bit of work involved here; not just a simple plugin install. With that in mind, I might procrastinate a bit longer, but will definitely have to get on it.

    Thanks for sending me the link; definitely a great read.

    Ana Hoffman

    Reply to this comment
    • Julius

      19. Oct, 2010

      Hey Ana,

      yeah me too. Well I tried to change it a little bit but it didn’t work out. Then I said to myself that I should try to create a better and nicer search (and write an article about it). That’s how I found out about all of this.

      There is definitely a bit of work involved here. But I can always help you if you want me to. If you simply want to install a plugin then check out WPSearch 2, Kenny mentioned it above. It might work better for you. Check it out and tell me what you think :)

      Reply to this comment
  6. Mike

    18. Oct, 2010

    This post will be so helpful to people, Julius. That’s the thing I also notice with WordPress but haven’t had a chance to cover it yet and you hit this problem right in its face, man. Great job :)

    One more thing that will be nicer if this search can keep the log of what keywords people use to search in our blog so that we can know whether our content has satisfy what readers are looking for. What do you think, Julius?

    Reply to this comment
    • Julius

      19. Oct, 2010

      Hey Mike,

      thank you, I am truly glad you like it. I hope you will be able to easily implement it. If you have any trouble be sure to contact me.

      There are several options in order to keep track of what people are searching for on your blog. The first one is to use Google Analytics. I’ve included a link in the bottom of the post which talks about how you can integrate site search for your blog. The other option is to use WordPress plugins. SiteMeter is such a plugin (I’ve also mentioned it in the bottom of the post). I hope this helps :)

      Reply to this comment
  7. Patricia

    18. Oct, 2010

    Thanks Julius. I don’t have a lot of articles as yet so can still manage to find all my posts lol
    But this seems a helpful tool once I do have. Thanks for such a comprehsive explaination and great screen shots of what to do. For those like myself who are not techies this sure helps.
    Patricia Perth Australia

    Reply to this comment
    • Julius

      19. Oct, 2010

      Hey Patricia,

      yeah but over time it will become much less manageable. You can just install Search Unleashed if you want to and then follow the steps I mention there to make it perform better searches. But I can always help you if you want to. If you want an easier solution then you should take a look at WPSearch 2 which is a new WordPress plugin (Kenny mentioned it above).
      Thanks for stopping by.

      Reply to this comment
  8. Kenny Katzgrau

    18. Oct, 2010

    Just launched at Wordcamp NYC: WPSearch 2 http://wordpress.org/extend/plugins/wpsearch/ — Search unleashed is okay, but it really doesn’t hit the root of the issue.

    Reply to this comment
    • Julius

      19. Oct, 2010

      Hey Kenny,

      thanks for mentioning the plugin. I will definitely check it out. Why do you think Search Unleashed does not address the root issue?

      Reply to this comment
      • Kenny Katzgrau

        21. Oct, 2010

        Hey Julius,

        The big things that I think most people care about when it comes to search are speed, accuracy, and relevancy.

        WPSearch is much faster than anything that utilizes MySQL’s fulltext search engine.

        WPSearch also performs things like stemming and stop-word filtering. For example, with WordPress’ default search, if you had a page named “Download”, but someone searched for “Downloads”, the extra ‘s’ would keep that page from showing in the search results. WPSearch stems works, and this makes for a true Google-like search.

        And most important, MySQL doesn’t score and rank results by relevance. It just grabs everything that matches in the database. So what might actually be most relevant to the user can very well be at the bottom of the result list.

        Search everything is definitely clever in the way it does things, but I think WPSearch is really the answer (and also, why we like to tell everyone about it!)

        Reply to this comment
  9. Michael

    19. Oct, 2010

    I agree with Alex and Steve in that you have hit this one out of the park! I am bookmarking too for future reference.

    Congrats,. on the killer post!

    Talk soon

    Michael

    Reply to this comment
    • Julius

      19. Oct, 2010

      Hey Michael,

      I am truly glad you like it. Once you have tried to implement these changes be sure to tell me whether it worked for you or not. I believe it worked for me (as you can see if you search my blog for something) but it’s not always easy to explain something to other people. Thanks buddy ;)

      Reply to this comment
  10. Sathish @ TechieMania

    19. Oct, 2010

    Hi Julius,

    It seems that you more of a coder than a blogger :P. Well thanks for sharing this useful plugin and the codes associated with it.

    Sathish

    Reply to this comment
  11. Tristan

    21. Oct, 2010

    I totally agree with you on the default WP search sucking. I’ve been using Search Everything and haven’t upgraded to anything else just because I haven’t wanted to spend the time on it. Thanks for the tips about Search Unleashed and Search Reloaded. I’ll for sure check them out in the future.

    Reply to this comment
  12. Thomas Borsutzky

    26. Oct, 2010

    Hätte es selbst nicht besser formulieren können! Danke!

    Reply to this comment
  13. jeffrey Morgan

    30. Oct, 2010

    Hi Julius,
    Man, you catch a Blogger off guard with this code! One of the better posts I have read this week I’ll have to admit. I’m just not prepared to deal with it right now. And I’ll agree, it needs to be addressed.

    I’ll be back when I have a couple of hours to deal with it.

    Reply to this comment
  14. DiTesco

    31. Oct, 2010

    I use Google Custom Search for all my blogs. It delivers well and I can set it up to search only on my network of websites. This is addition to the fact that it is linked to AdSense for search made it my choice. Now it does not mean that this plugin is great and as a matter of fact I will test drive it on one of my websites and compare results with that of Google CSE. Thanks!

    Reply to this comment
  15. Ed Washington

    02. Nov, 2010

    The link to AdSense is definitely what puts Google Custom Search over the top for me as well.

    Reply to this comment
  16. Dean Saliba

    09. Jan, 2011

    I don’t like the default Wordpres search. Last year I wrote a tutorial about how to intergrate the Google search into a WordPress blog. Works well. :)

    http://ineeddiscipline.com/2009/11/19/adding-google-search-results-to-your-blog/

    Reply to this comment
  17. Ryan

    28. Oct, 2011

    The default WP search is absolutely blammy. I completely agree with this. Great tutorial, when I find some time Im going to check it out, credit you and give it a review

    Reply to this comment

Leave a Reply