Advanced Custom Fields combined with Ultimate WP Query Search Filter

If you’re like me, you love Advanced Custom Fields, for which I use the Pro version. I use it 99% of my projects because it’s just one of the best plugins to create custom meta fields for all sorts of post types and terms.

Another nice plugin is Ultimate WP Query Search Filter which allows you to easily create search forms with all types of meta/taxonomy queries. It works smooth out of the box, except for one thing, which was the core of my issue.

The issue

Searching in an ACF checkbox field with UWPQSF is impossible by default due to the fact that ACF stores its values in associative arrays, which UWPQSF doesn’t like since it assumes single values are used/stored.

The solution

Since UWPQSF can only search in single values out of the box, we need to reformat the search query so it accepts an array of values.

This solution works for me and is a tailored to my project, but it gives you a very good jumping off point if you want to achieve the same with different values/fields.

Introduction

For this explanation I use a ‘model site’ featuring profiles from models. You can search for city, sex, age and what kind of services they provide (hand model, swim suit model, fetish model, etc) where services are the checkboxes.

I use the ACF front-end form to let users submit a post (which is the profile). This post has an ACF called sex which is a taxonomy, which needed to have a condition logic option. ACF doesn’t offer this default so I created a custom option which enables this. This is explained in this support topic.

Since my taxonomy is now a meta field, the tax query is not used in my function below.

Steps to take

  1. prepare the UWPQSF search form
  2. create a function to override the search form
  3. retrieve the used search query before it’s executed
  4. place the meta_query in variables (optional)
  5. check if a checkbox is used in the search query and how often
  6. if more than 1 box is checked, we need to alter the query to accept more than 1 value
  7. get all the search values we don’t need to override (optional)
  8. get posts with new query
  9. feed our new parameters back into the search query

1. prepare the search form

For this example I use a form with 4 different meta fields, of which the first three are dropdowns and the last one is a checkbox. You can add the dropdowns like you’re used to. The checkbox is a bit special.

UWPQSF comes with a ‘general value’ option, which lets you create values for all custom values which are used by posts. If you generate the values with an ACF checkbox field, the results looks something like this:

But there is a way around it. Go to your ACF settings for this specific field. Copy all the values from ACF and copy it to a text file. You now have a list something like

Reformat that list so it looks like this:

Insert that line of code into the field where the generated values are placed. Then add the field.

2. create a function to override the search form

We start by adding the following code to functions.php. This is the default function to display any search results. With this in our functions.php we can now safely override the display of the search results but also manipulate the query which is used to search which results are shown.

3. retrieve the search query before it’s executed

To retrieve the search query which is used we var_dump() the $arg variable which contains the search query, followed by an exit, which stops the rest of the code from being executed.

Add lines 2 and 3 of the following code to the code we added above.

UWPQSF search query
Search query dump
If you then do a search you see a nice ordered query of what variables were used in the search query. It looks like the image next to this text.

Since we only want to override the meta_query, which holds the search parameters for the meta queries and thus the checkboxes, the rest doesn’t really interest us.

The screenshot shows 2 dropdown options where no option is selected, aka all options are searched. See the value ‘get_all_cmf_except_me’. If you play around with your search options, you will see the values change in your output.

By using one of our meta fields, we now know which array is the checkbox. In my search form it’s the 4th (and last), which looks like the code below where 2 values are selected.

NOTE
If only 1 meta value is selected, the returned value is an array with 1 value (so lines 9 and 10 won’t be there in the screenshot above). If no values are selected at all this entire array is omitted from the query.

Reload the page and search the form with using at least one checkbox variable. Copy the entire output to a save place. We’ll need this later.

4. place the meta_query in variables (optional)

I prefer to have my values in ‘easy to understand’ variables, so I retrieve the values from the meta fields like this and place them in my own variables, which I can then easily re-use later on.

5. check if a checkbox is used in the search query and how often

To check if this checkbox is actually used, we need to check if the value is set. My checkbox was the fourth in the array, so we check if the 4th value in the array is set. And if it is set, we retrieve the values and place them in some custom variables to use later on.

Ok, now we know one or more options are selected but we need to find out how many are selected. We do this with this function. Add this to the code, where it says more code to come.

6. if more than one box is checked, we need to alter the query to accept more than one value

Since UWPQSF only accepts a string, we need to turn the array into a string if only one value is selected.
Replace // do stuff when one value is checked with the following code:

If more than one value is selected, the search value is an array as mentioned in step 4; $services_value. Since UWPQSF only accepts a string for checkboxes, this array needs to be split into several arrays with one string/value each, because the meta_query accepts an array of key/value pair arrays as value, as described in the WordPress Codex.

So we take $services_value (which is an array, remember) and with foreach we create a new array for each search term, which we then again add to another array, which we can easily include later.

7. get all the search values we don’t need to override (optional)

This next step is not perse necessary but I explained in step 4 why I do this.

8. get posts with new query

If a checkbox is used, then $services_key is set (see step 4), so we can easily check this with the following code.

Now we need to build our new search query which we can use with our new variables and place it into a new array, which I have called $new_args. This will look something like this.

Lines 2-8 are the search parameters which are set in your UWPQSF settings. We retrieved these values in step 6.
Line 28 is the string search option, which I don’t use, hence it’s empty.

For the meta_query you see 4 different arrays; city, sex, age and the $services_array we built in step 5. These represent the 4 search options I set in my search form.

Replace // query for when checkboxes are used with the query above.

If no checkboxes are used, then we can use the original query. Replace // query for when no checkboxes are used with the query below.

9. feed our new parameters back into the search query

One last thing to do. We need to feed our parameters back into the query used by UWPQSF.
Replace

With

And there you have it. You should now be able to search in checkbox fields as well.

If you click to the next page, you’ll find a complete dump of my file. I placed all UWQPSF functions in a file which I included in functions.php. This is a dump from that page, minus the part in which I display the actual results, since it’s unrelated to this issue.