Understanding and Fixing WordPress Search

There is a lot you can do with WordPress regarding searching your blog. You can use the built-in search capabilities, replace it with a search from Google or Yahoo (or whoever), or use an awesome WordPress Plugin called Search Everything to really get full advantage of searching your blog.

Let me explain what and how the built-in search function works with WordPress, for those left confused and frustrated as I was.

How WordPress Search Works

When you search in your WordPress blog, your search results are listed chronologically. Not by “most likely”, “most popular”, “most frequent use of the phrase”, or even alphabetically, just by date. And the chronological order runs from most recent to oldest. If the most likely post to provide the information the user is searching for is older, they will have to scroll towards the end of the list to find the most likely candidate for information. What are the odds they will, huh? This frustrates me no end.

Another frustration with WordPress searching is that it only searches posts. It does not search comments nor Pages. Only post content.

Another thing happens internally that the user never sees, but you, the administrator and/or Theme designer, need to know about. By default, there is a hierarchy to the order of which template is used for which request. A Theme may, or may not, have these optional template files to generate specific web pages, page elements, and looks. In the case of search, when the user clicks the SEARCH button, WordPress looks for a search.php template file. If it doesn’t find it, it will generate the search on the index.php template file.

You can see examples of my main website’s custom index.php page and custom search.php page to get a feel for how these can be customized. The index page features huge excerpts and graphics, while the search results excerpts are shorter, but look similar in design.

A Theme doesn’t have to have a custom search template page, but if they do, it allows customization of how the search results look and feel. Nothing has to be changed within the rest of the templates in order for a custom search template to be added. If you want to add a custom search template, simply copy the search.php template file from another WordPress Theme, or make a copy of the index.php template file and rename it search.php, then style it to match whatever you want your custom search page to look like. No special coding is required. WordPress will automatically find it next time you go to search. Hopefully. ;-)

For more information on customizing or creating your own search page, see Creating a Search Page in the WordPress Codex, the online manual for WordPress users, designers, and developers.

Replacing the WordPress Search

To replace the built-in WordPress search functions with a search engine search function, simply sign up and get the XHTML codes for the search engines search function and edit the searchform.php to remove the built-in PHP search functions and replace them with your new code. The WordPress code looks approximately like this:

Be sure and test the search engine search functions thoroughly, and style the search engine search to match the look of your WordPress Theme, if necessary. Some search engine functions will allow you to specify a search results page, so if you can, why not make the search results look like your WordPress Theme by generating them on a search.php template file to match the overall look.

The Search Everything WordPress Plugin – Easy Search Expansion

To get around the limits of the WordPress search functions, without replacing it with a search engine search form, the Search Everything WordPress Plugin expands the search results to include comments and Pages, though currently it also returns only chronological results, from most recent to oldest posts.

To install Search Everything, upload the plugin to your wp-content/plugins folder and activate it through your Plugins Administration Panel. That’s it. It works. If you have messed around with your searchform.php template file, you may encounter problems, so check through the comments on the Search Everything WordPress Plugin page for more help, or ask.

67 Comments

Nice article Lorelle. I still find the search insufficient because WP allows you to do many more things by its functionality. But the search is not up there yet, e.g., I will not be able to search in any data that I create which is not wrapped in WP posts. It currently does not search through excerpt of posts or the other custom fields. While changes can be made to make WP search everything, it depends more on the types in the MySQL database. It would be more flexible and efficient, if it is built into WP design. We can then even have something like an Advanced Search or a filtering process.

You’ll have to check with the various search engines services, like Google Search or Yahoo Search, and find out how they allow you incorporate their search functions within your WordPress Theme. I may be doing that in the next month, and I’ll report on this then.

Does anyone know exactly where the code for the search is? I’m looking for the code that actually does the MySQL fulltext search for the posts. I’ve found the searchform.php file which spits out the search input field and the button. I’ve also found search.php which is the template for displaying the search results. But, I’m looking for the actual php/mysql code that does the searching. Thanks!

I ask this because the adefault action really isn’t a
file.php but only a folder so I not understand how it work.
what is the file that treat the variable s;
identical for your suggest (..PHP_SELF), the file is the same of the form, but if so you can to do examples of code for treat the datas

Ah good catch. I posted the customized PHP code from an old default WordPress Theme version. I’ll change it to the new, and more appropriate one.

As the instructions say, sign up for a new search engine searching service and you will be given the code to use and search for the above code and replace it with your new code.

Thanks for the catch. See Creating a Search Page in the WordPress Codex for more information on the search functions. As for your answer, that was the old method, and this is the new, replacing PHP function with WordPress template tags, a much more powerful usage. The result is basically the same.

Hello Lorelle! … Your post is really interesting.. but I have been trying to find (with no luck.. maybe you know about this) what is the algorithm used by the WordPress Search System… also.. where I can find its search function?.. does it have the capability to order the results by relevance?…if it has it.. how does it make this relevancy ranking?… I have checked the DB and it doesn’t create any search-related table… :) thanks for your response in advance..

Thank you so much for posting this Lorelle. I have had a lot of trouble with my WP search. Actually it doesn’t return any results for anything I search for.

When I build themes I do it on another dev site, and the searches work fine. But when i bring that theme into another existing site and start using the theme, the search doesn’t work. I have had this happen on 3 or 4 different themes.

i thought that maybe i had tweaked something a long time ago that inhibited it from working so I downloaded the newest WP (2.3.2?) and re-uploaded everything and then added in my theme again to get a fresh start, but the search still isn’t returning any results.

I am going to try your suggestion for the “search everything” plugin to see if that will help.

if by chance you might know why I’m not getting any results I’d love to hear about it. But at least your article gives me hope.

My default wordpress search doesn’t work, I’ve tried many plugins and always get the same result … “No posts found. Try a different search?”. Can anybody help me please? I’ve been through the wordpress support forum many times and can’t find anything that would help.

I recommend you try a different WordPress Theme. If the search works, the problem is with the Theme. If it doesn’t work, do a total clean reinstallation of WordPress (backup everything first, delete database, remove everything from the server. Then install.) and see if it works then.

If it doesn’t, consider checking with your web host as there may be some glitch on their end, though unlikely, and then keep asking on the WordPress Support Forum as they are your ONLY source for such help. Plugins aren’t the answer.

The Ultimate Tag Warrior WordPress Plugin is no longer supported. If you are using it on your blog, that means you have not upgraded your version of WordPress. If you have not upgraded, you have been ignoring the very publicized mandatory security upgrades. WordPress blogs not upgraded have been hacked as trouble makers are seeking out those versions.

Upgrade immediately. The latest version of WordPress includes tagging and has an import feature for UTW. There are numerous Plugins, some by the same author, for the new native tags.

Lorelle,
Thank you! This indeed was exactly what I was looking for. I’m seeing that only post content is searchable. I can search for and find categories, BUT if I search for Uncategorized I get no hits even though I have several. Now that I think about it though, when I checked out the database, there were to taxonomies. I wonder if Uncategorized and my other categories were of such that the search only hits one taxonomy? Hmm. Well, at least I have an idea of the scope of the search, and no longer worried that it’s broken. I just had to fix a broken theme that I like but gave a 404 page not found whenever you click the Search button. With your info, I can move on to other tasks and have faith the search works as it was coded for.
Jason

If you are looking for posts within your “uncategorized” category, use the Manage Posts panel and set it to filter posts by category. That will show you everything in that category. Edit each one and put them in their right category.

I do not know whether anyone has noticed this. The search function in my blog comes with the theme I’m using and everytime I do a search, the number of results I got is exactly as the number of posts I set at the Reading Settings. That is, if I set my blog to display 5 posts only, the search also produces 5 results. It’s annoying, somehow.

I have both Google search as well as site search, but whereas the the former does indicate “Google Search”, the site search (WP widget) simply says “Search”. I’d love to change it to “Site Search”, so visitors are not confused by the two search boxes.

Lorelle, I’ve been trying for hours to figure out how to get search results to display full post results (or even excerpts) on my Kubrick Theme. Right now, I just get headlines, which makes it difficult to discern the content within. So far I found this: http://wordpress.org/support/topic/213354 but when I look for the elements it describes, they don’t exist. I really like kubrick, and use it as a private blog, so I would like to continue with it rather than switch templates. That being said, if there’s a search.php template that I could use, it would be great. I liked the example you give, but am not sure where to find something like that.

Is there a way I can easily change the kubrick template and get these results? Thanks!!

Thanks for the article. I have a query regarding the variable name used in the searchform.php. With the statement
<input type="text" class="txt" name="s" id="s" value="" …
the URL is shown as
mydomain.com/?s=searchstring

I want the URL to show like
mydomain.com/?keyword=searchstring

so I changed the above statement to
<input type="text" class="txt" name="keyword" id="keyword" value="" …
but now the search won't work!

I also changed all references to 's' in
…/wp-includes/query.php
to 'keyword'
but to no use.

I just wanted to say a big thank you to you for this wonderful post. Google’s custom search ain’t compatible with my blog as the search results always spill into the sidebar. With your knowledge I’ve been able to create a search.php with adsense added to the top and bottom of the search result page. I’ll be trying section targetting on it to see if the adsense ads stay relevant to the search keyword. Once again thanks for a great post.

I recently found a great little plugin called Relevanssi that will order the WordPress search results by relevancy rather than date. It’s worked out well for me so far, and it even inspired me to include it in my most recent CMS plugins post.

Wow, I’m sorry you built your site on Pages. That’s so painful. I’d check with some external search features to add to your site as what you really want is a search within the search. I wouldn’t restrict by Pages, Categories, etc., just a search within search. Simpler for the user. Google, Yahoo, Bing, and Legit have those options in their search add-ons.

If you get smart and rethink the whole Pages versus posts thing, there are some WordPress Plugins that will help you convert Pages to posts and the reverse.

Good Morning Lorelle. The reason for pages instead of posts is that mine is a directory site for a large school. The senior staff, along with each faculty member (over 275) has her/his individual page that, for now, I do all of the updates myself (ouch). The homepage sidebar and search serves as a global directory listing and search. The hierarchical structure also divides the faculty and staff according to position (administration) as well as class section (9) to which I would like to provide an individual search option to each.

The reason for pages versus posts allows for the alphabetical directory listing that posts do not allow.

Of course, ANY ideas and suggestions would be hugely beneficial here. I am not too deep in the implementation that I can not change up.

There are membership Plugins you can use to make your life easier. For search, I’d use what I recommended, then look into the Membership and Directory Plugins and try the Officers Directory WordPress Plugin to seriously make your life much easier. And Mikko’s recommendation is one option.

Pages and Posts can be anything and maybe what you really need are custom post types not Pages, which are Pages, but treated differently in the taxonomy which would actually be better for you as you could use any of the sophisticated bread crumb Plugins that deal with custom taxonomies. That would be smarter.

The Officers Directory creates a custom list of faculty, directors, employees, whatever, with links to their pages or whatever you want to do. The author, Douglas Bell, is in college and did this for his college associations. If you needed it customized even more, ask him. I’m sure he’d jump at the chance to make a few dollars to offset his expensive college bills. What I love most is that if you have an opening or vacancy, you can put that in fast and easy. It’s excellent for controlling employee lists and such.

As for what you are doing, you are trying to reinvent a wheel that has been invented many times before. WordPress is commonly used as a directory and membership. Find out what they are doing. Consider going WordPressMS and give everyone a blog, which they would have to keep updated with their own information rather than you do it, and give them their own class posts and ability to do all kinds of fun things. There are so many options, why not explore all the possibilities out there with WordPress. And check out all the great Plugins that can help make your life easier.

Or just keep doing the legacy work you’ve been doing and just add a custom search with the option to search within the search. That’s simple if you are entrenched. I like playing smarter and giving up on legacy stuff as fast as I can if I know it’s broken.

This project was thrusted upon this WP newbie because they can both require and expect this of a lowly IT (hardware) techie in this economy AND they will not spend money that they don’t have. But alas….

Since having been handed this assignment, I’ve been crash-learning WP so “…custom post types not Pages, which are Pages, but treated differently in the taxonomy which would actually be better for you as you could use any of the sophisticated bread crumb Plugins that deal with custom taxonomies.” are somewhat new to me… right now anyray.

I’ve already contacted bot Douglas and Mikko for ideas as well.

What they REALLY want is both… I manage certain sections of their ‘pages’ and they register and manage the rest of their own post… pages… whatever.

What I need is a ready and functioning solution that I can reconfigure and make my own.

I totally understand. Douglas and Mikko are great and will probably help you plenty. Good luck with it and know that you are not alone, so look for others who’ve solved this same issue and learn from them. They are the ones who invented the wheels already. :D

Hey, My search function displays “No relevant search results found” on every query. Not too sure how to go about fixing this issue. If you could give a look and see what you think the problem is I would greatly appreciate it.

This is an old post and WordPress has fixed their search extensively. If you would like to hire me for testing your site, please use my contact form for such a request. I highly recommend you seek help on the WordPress Support Forum first. Without more specific information, I and others could not help you as the search could be broken within the Theme, with some Plugin that interferes with the search functionality, or your site may not be properly installed. It is rare for search to not work. Good luck with it.

Hello There!
My Theme is having search.php code linking from index.php by ‘if’ condition for search, category, tag etc. But when i am searching anything, it is showing random posts rather then the related search results.
Can you provide me the default code to insert at search.php to unlink that main index and create related search results ?

Contact the author of your Theme or the WordPress Support forum. The search page is generated by default in WordPress. No special instructions are necessary. If the author has done something different from normal, please contact them as they must have a reason or ask in the WordPress Support Forum for assistance as someone may be familiar with that Theme. Be sure and name it in the title of your question.

Changing the core Theme makes updating the Theme difficult, as you may have to recreate what you did. Having the Theme author fix it is the best path. Thanks.

I was wondering, though, if you could add dates to your articles. Based on the comments I guessed this was written in 2006, but it would be helpful to know that information right when I begin reading the article. Even if the article is as old as 2006 I’ll probably read a portion of it since I like to know the history of the code I’m working with, but the date will let me know if I need to look for newer information. (If an article is from 2006 I’ll probably look for new information). I find a lot of blogs that don’t date their articles (if you do it for SEO reasons, I hafta tell ya it doesn’t work!! Google and Bing know when you posted this thing ;) ).

[…] those battling the challenges of providing a decent search function on your WordPress blogs, the hunt for the ideal WordPress search is still on. It has improved, but so far, I haven’t found a perfect search WordPress […]

[…] now that that thinking was rather narrow. Lorelle Van Fossen has a neat post on why and how, on a WordPress site, you would think about replacing the default tool with a third party search tool (such as, but not […]

[…] by an ever-changing Relevance vs. Time calculation. Now, one of the most common complaints/tutorial topics about WordPress involve improving search via extra code, plug-ins, or a better results page. Since […]