Better WordPress Searches

WordPress developers worried about messing around with the ‘hardcore’ bits such as the search will be delighted to hear that making changes to the way the search works is logical and pretty straightforward too.

The two main elements of a search; the form and the results page; can both have their functionality altered within templates.

This is surprising to those new to wordpress development who are taking their cues from the default 2011 or 2012 templates, since nether of these include a specific file for the search.

However, the index page is the default search results page, so make a copy of index.php and name that copy search.php  and this automatically becomes the page that delivers the search results. Amendments to the loop on your new search.php page (combined with stylesheet changes) will directly affect what the visitor sees.

You can use this to add div tags and classes for formatting, remove unwanted content or add additional data to results.

You can also restrict your search to certain elements of your site too. I have a client whose wordpress ‘shop’ uses posts as products and so she doesn’t want ‘pages’ to appear in the search results. To filter the searches before they are run you can add a hidden field in the search form.

This takes the format

 <input type="hidden" name="post_type" value="post" />

On the default templates default code for the search form itself is hidden away in an include somewhere, but again, by simply copying the html code and placing it in a new template file called ‘searchform.php’ you can have instant access to that file and add in the ‘hidden’ filter.

So once again, thank you WordPress for delivering a system that is clean sensible, and infinitely adjustable. No wonder that WordPress is now the world’s biggest publishing platform.