How To Create Amazing Search Functions For Your WordPress Blog
Create Better Search Results With These Handy Tips
A lot of people have been complaining over and over again about the bad WordPress search. Instead of complaining let’s simply create a nicer one which delivers better results. Before I go into the details I just want to note that most of the code below is taken from Joost de Valk’s excellent article about creating a better search. Be sure to take a look at it if you are serious about creating a better search.
The biggest problem people have with the default WordPress search is that it simply doesn’t pull up the results that are most relevant to the query. That’s the first problem we are going to address in this tutorial. The last part of this article simply shows you how you can make the search even more useful by using some cool tweaks.
This article was originally published at And Break but has been republished with Julius’s permission.
More Relevant Search Results
The default WordPress search sucks and that’s the reason why I started to search for alternatives and came upon Search Unleashed. Search Unleashed is a litte bit more complicated than other search plugins but if you follow my advice it should be easy to set it up.
An alternative to Search Unleashed would be WP Search 2 (free & premium plugin) as well as Relevant Search, which is used by all the Tuts+ sites. All of these plugins deliver more relevant search results so it’s up to you to decide on one. The reason I stuck with Search Unleashed is because it’s extremely customizable. It also returns relevant search results as long as you know how to use it. It’s also free.
Basically Search Unleashed let’s you create your own search engine by giving you the option of deciding on priorities. You can do this on the Tools -> Search Unleashed -> Modules page.
If you, for example, think in order to get relevant results the keyword should be contained in the page title then you should assign this page title a high priority in order to reflect this. I chose 1 to tell the search engine that it’s the most important. You then go ahead and click on the things that you think are further important too, such as category, content, page excerpt, tags etc. and decide on a priority.
Configuring Search Unleashed
The priorities are as follows:
- Comment content: 0.1
- Post category: 0.5
- Post/page content: 0.7
- Post/page excerpt: 0.3
- Post/page slug: 0.2
- Post/page title: 1
You can actually see how the search is configured and the priorities assigned to each type. For me this combination works best but you can change it to whatever you want too. Once you have set the priorities be sure to save the settings.
Before any search results will be shown on your search page you need to go to the Search Unleashed page (Tools -> Search Unleashed) and re-index your site. This might take a few moments.
The next step is to change some additional options within Search Unleashed. The problem with the standard settings of the plugin is that it makes the search results extremely ugly. More importantly the search function hasn’t changed yet so it doesn’t deliver better results.
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 (which we know sucks). 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 you need to do is to scroll further down the page and to change the color of Highlight Color #1 to #FEFFBF. That way the highlight is not too bright.
Create Better Search Excerpts
In order to get nice search excerpts you need to install the Search Excerpt plugin. All it really does is to create a nicer text excerpt of your article and highlighting the search terms. Once installed, activate it and it will show nicer excerpts automatically.
Style the Search Results Nicely
To get a nicer looking search page we are going to highlight the keyword within the heading of each search excerpt in order to make it stand out. A great addition to this is to also show the total number of relevant pages. 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);
$allsearch = &new WP_Query(“s=$s&showposts=-1”);
$count = $allsearch->post_count;
wp_reset_query();
?>
The first few lines of code bold the search term in the headline and the last few lines are there to count the number of relevant articles.
Now add this code to your css file (Appearance -> Editor -> style.css):
strong.search-excerpt { background: #FEFFBF; }
Then save the file. To actually show the number of articles we need to edit the search.php file further. In order to do this find this code
<h2>Search Results</h2>
and modify it to:
<h2>Search results “<em><?php the_search_query(); ?></em>” –
<?php echo $count; ?> Articles</h2>
Be sure to also modify the “no posts found” line below the endwhile; code in the same manner.
Refine the Search Result Page
To refine searches you should put another search box below your excerpts. This will give the user the opportunity to refine his search when he hasn’t found what he was looking for within the first few search results.
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 add a number of related searches in order to make it easier for the visitor to find what he is looking for. To do this you need to have the Search Suggest plugin installed. Then simply add this below the code from above:
<?php related_searches(); ?>
Change the Pagination Names
To really create a better user experience on your search page you should change the pagination links. They are usually referenced after the endwhile; in the search.php file. Change the normal pagination link names, like Older Entries and Previous Entries, to Previous Page and Next Page to make the page look nicer.
Change the Search Form to Include the Keyword after a Search
In order to make it easier for users to refine their search results you should change the default search form value. Google, for example, always keeps the keywords 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
Normal search engines catch typos too so why not do it on your blog? Search Suggest can suggest keywords when someone misspelled a word. In order to make use of this add the following line below the “no posts found” code within the search.php file:
<?php spell_suggest(); ?>
Track Site 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 searches which is commonly used. If you do so then you will be able to find the search reports under Content -> Site Search in your Analytics dashboard.
There are also WordPress plugins that allow you to do this. Search meter is one of them. If you are more comfortable with that I would recommend to stick to that.
"Do Not Write Another Blog Post Until You Watch This Free Video..."
Watch this free video to learn...
- How I got over 10,000,000 people to visit my websites.
- The types of blog post that got me all that traffic.
- How to get someone else to do it for you!
Excellent post once again! Implementing the Google AdSense for Search is also worth mentioning since it delivers relevant results and can also generate some revenue for the webmasters, by showing targeted ads.
Yes! This is what I need. Thanks to you and Yoast… seems like you both are at the top of my must-read list for blogging!
@Gabriel – Yea, Adsense search is really worth trying and it has been doing good for my blog.
Anyway, Thanks Dunlop, for sharing this simple yet powerful info
Nisha
Good stuff. I never gave much thought to doing something with the search bar on my blog but you have shed some light on things for me.
I’ll be putting this to use today.
Superb post no doubt about it, but instead of using this plugins how about using google custom search functionality.
I think no plugins can work better than google search technology. Though I am not using it at my blog yet but blog having 300+ post must.
Hope some find this comment useful.
I don’t ever include the search function in my templates, because I didn’t think many people use it. I guess I am basing that on my personal experience, as I don’t use the search function ever myself.
Do you think it is important to include on your site?
Awesome post Mike! I’ve being thinking about this lately but don’t really know how to go about it. I will implement this on my blog. Thanks for sharing! Rock on
Another good post Michael! I’m going to put that on my blog…I like the way you add all the snippets of code because I am almost code illiterate lol and giving the code like that really helps me out
Your friend,
Eddys Velasquez
Hey Michael, what a fantastic post, so helpful rather than telling us to improve the search – actually step by steps
Many thanks Michael and I look forward to your future posts
Steve
(I’m still stuck on setting mine up as came across Optimizepress which has thrown a spanner in the works and have got rather confused with it and trying to work out the best way to work it – or not at all)
Do you have any experience with OP ?
I’m fairly new to all this and therefore have no idea why you would do this.
I never use the search options on a blog post, do many others?
Sorry to be dim…..
Wow thanks for that. I installed Search Unleashed and it is great. I don’t know what wordpress were doing when they setup their search function but it must have been close to knockoff time:)
Thanks for the input. I’ve been looking for something like this to use for my blog.