Making WordPress Search Better

In this post I’m addressing a shortcoming of WordPress’ built in search function.

Notice

ADDENDUM: As of, well, sometime between this writing and 7/2016 the plugin JetPack has added this as a feature. I don’t know they do it the same way, but the following article is no longer correct when it states that WordPress does not handle special characters or that it limits it’s searching to only blog posts.

Even though this information is obsolete, I’ll leave it in place for historical purposes.

If you create a page or post using characters which are deemed “Special” such as <, >, &, etc. they are automatically stored as their html coded equivalents, &lt;, &gt;, &amp;, etc. This makes sure that the text is properly encoded for browsers to display.

Makes sense so far, right?

Well, the snag is that if you want to SEARCH for this text, it won’t work. A post containing “This & That” is stored as This &amp; That but when you type “This & That” in the search box, it retains the “&” and does NOT transform it to &amp; and the search comes up empty.

The following fixes that. Open your theme’s “functions.php” and paste in the following code.