How to create an advanced search form for WordPress

“Yes, so what you should have is an advanced search form where people can search for posts based on multiple criteria”

So that’s what I said to a client, na├»vely thinking I could find a WordPress plugin that would search from posts based on a date custom field, a category AND a keyword.

Unfortunately, while there are a lot of search form plugins for WordPress that do some of these things, I was unable to find one that did all of them.

In the end, I had to make one from (more or less) scratch, and while I found a couple of tutorials that gave me hints on how to do these things, none of them covered everything I needed to know.

Eventually, after solid days of PHP hell, I had figured it out and vowed to give something back to the world by showing how I did it.

What we’re aiming for with this tutorial

Basically, we’re putting together a search form that looks like this:

searchform

It searches based on a total of 3 main criteria (technically 4):

Course Type: This is a list of sub categories of a larger ‘Courses’ category. As the webmaster may want to add different types of courses, this list needs to be generated automatically and then placed in a dropdown menu.
Course Date: This searches based on the ‘course date’ custom field generated using the excellent Advanced Custom Fields Plugin. We also want an extra piece of logic that determines whether to search for courses before, after or on this date.
Keyword: This is your standard keyword search.

Leave a comment