Custom WordPress search combining multiple tax queries & meta queries

23rd April 2013

Recently I was working on a project for a client of ours and had the need to build a new multi search function. I came across some interesting new things about WordPress I thought I’d share with you and might make your search for a solution a little quicker as this solution is a combination of several different tips I came across online and from my awesome colleges (Dan & Daniel).

What the search should do

Let me first tell you briefly about the project. We are creating a separate section consisting of a few pages, for a bunch of sites in a WordPress multisite. The section’s purpose is to display a database of staff members in an interesting, attractive way. But the user also needs to be able to search among the people in the database based on a variety of search criteria.

This is where I realised that the built-in WordPress search would not suffice. There is a load of plugins out there, but unfortunately none of them met my particular needs. In short I needed a search function that;

  • Allows me to chose the input form (mostly free text fields) and not dropdown menus which was commonly offered by the plugin options when it came to taxonomy for example.

  • Allows me to mix between several taxonomies and custom fields all in a particular custom post type and combine them all in one search.

  • Ignore search fields with empty values in the WP Query

Since the project hasn’t of yet been launched I won’t give you specifics of the data involved. But for the purpose of explaining what I needed, let’s say the database consisted of medical staff members and the pages can be found on a hospital website.

The search involves these criteria:

  • Free text search of staff name

  • Free text search of occupational title

  • Free text search of hospital department

  • Free text search of Expertise area

  • Dropdown list of years which would correspond to the year from when staff started working at the hospital and upwards. (only currently employed staff is in the database)

Staff name and start date are both taken from Custom fields in the WP backend. The Occupational title, Hospital department and Expertise are all Custom Taxonomies.

WP Tax query limitations solved

The first issue I ran into was trying to combine different Custom Taxonomies in a WP query, as putting them all in the query using the variable with the search value in it limits the relation you can use between the different tax terms. Also, sometimes the value saved in the variable will be empty and since no taxonomy value it’s compared with will ever be empty, then the results comes back empty even when the other Tax Queries might have matching results.

When using taxonomy you are limited to comparing the value from the search query with either the slug or the ID. This didn’t really work well with a free text search as you might imagine and also when search values were received from other places, ie a link.

First thing we do is to check if the search field value is empty or not – if it’s empty it’s useless in the query. If it has a search field value (which we would have caught earlier on in the file and saved in a variable named $staffTitle) then we do the following:

  1. Get all the values of the relevant taxonomy and save into a variable

  2. Count all the terms’ characters and compare if the count comes up with an existing taxonomy value including the characters in the search value provided.

  3. If it has a corresponding value (case-insensitive) then we go ahead and save down the slug of the taxonomy value in a new variable ($staffTitleResult[]) to be used in the WP_query later. This became particularly necessary as we also have values coming into the search form from other places. (which won’t be covered in this post but you can request if you like)

And in PHP code that would be:

1

<br /> if (!empty($staffTitle)) {<br /> $staffTitleResult = array();<br /><br /> $terms = get_terms("staffTitle");<br /> $count = count($terms);<br /><br /> if ( $count > 0 ){<br /> foreach ( $terms as $term ) {<br /> if (stripos($term->name, "$staffTitle")!==false) {<br /> $staffTitleResult[] = $term->slug;<br /> }<br /> }<br /> }<br /> }<br />

This solves the problem with not being able to compare the search result with the taxonomy value’s name and also ignore any empty search fields in the search. Just replicate the above code for additional checks for any other custom taxonomies you have.

Array to correctly compare multiple taxonomies

We then need to go on and create an array to handle all our 3 tax queries (since we want them to be compared with each other if they all or some of them have a value)

Now we check again to see if search field value is empty, if it’s not then we fill our array with the particular taxonomy info we want to compare with the variable we created in the previous section.

1

<br />$tax_query = array();<br /> if (!empty($staffTitle)) {<br /> $tax_query[] = array(<br /> 'taxonomy' = 'stafftitles',<br /> 'field' = 'slug',<br /> 'terms' = $staffTitleResult<br /> );<br /> }<br />

We then add the tax_query array to the arguments for the WP_query:

1

<br /> $args = array (<br /> 'post_type' = 'hospitalstaff',<br /> 'relation' = 'AND',<br /> 'tax_query' = $tax_query,<br /> 'meta_query' = $meta_query,<br /> 'nopaging' = 'true',<br /> 'orderby' = 'title',<br /> 'order' = 'ASC'<br /> );<br />$staffQuery = new WP_Query($args);<br />

Meta queries added to WP Query

As you can see in the arguments there’s also a meta_query array that has been added. These are our Custom fields that are being checked. As with taxonomy, we first create an array for our different “tests”. We check each search value field and add whatever search values for our query that we need and add to your array, like this;

1

<br /> $meta_query = array();<br /><br /> if (!empty($staffName)) {<br /> $meta_query[] = array(<br /> 'key' = 'staff_name',<br /> 'value' = $staffName,<br /> 'compare' = 'LIKE'<br /> );<br /> }<br />

Goes without saying, the number of custom field queries you can fit into the meta_query is unlimited. This solution works around the limitations in a straightforward WP_query and makes a mix of multiple taxonomies and custom field values do-able.

This search does a lot more as well, so there might be follow ups to this post if there’s an interest to see how this custom search was also made to accept values from links on the individual staff members page as well as the free text input option. And also if a search comes back empty it automatically inform the site’s owners and requests additional information be added to the database using wp_mail(). Could probably also write a few lines of how the custom field datepicker value for each staff member was compared against the dropdown year value in the search.

Let me know below if there’s an interest! But also if you have done something similar and/or have suggestion of different ways of doing it, improving it etc – give your comments in the section below!

Want us to help grow your business?

Give us a call, jump on a chat or come by our office in London. Our door is always open.

Contact