Using Multi-Select ACF in Query String

I have a CPT (instructors) setup. That CPT has an ACF that is a single-select radio button (“Group”) and one that is a multi-select checkbox (“Division”) with options like “dcl” and “cas.” In my looper, I want to include only those posts where “admin” has been selected in the “group” field and “dcl” has been selected in the “division” field. I’ve written this as:

post_type=instructors&post_status=publish&order_by=last_name&order=ASC&posts_per_page=-1&meta_key=group&meta_value=admin&meta_key=division&meta_value=dcl

The part that’s not working is the "meta_value_in=“dcl”. It works fine with just the “group” meta key, but not when I add the “division” meta key, and I’m assuming that’s because that fields returns an array.

If I’m correct, what’s the right syntax to indicate I want it to match just one of the values in the array?

Hello @dannikb,

Thanks for writing in!

You will need to use the correct arguments for your meta query.

$meta_query[] = array(
    'relation' => 'AND',
	array(
		'key'     => 'group',
		'value'   => 'admin',
	),
	array(
		'key' => 'division',
		'value'   => 'dcl',
	),
);

$query = array(
  'post_type' => 'instructors',
  'post_status'  =>  'publish',
  'orderby' => 'last_name',
  'order' => 'ASC',
  'posts_per_page' => '-1',
  'meta_query' => $meta_query
);

Please check out this thread to know how to get the proper query string:

Kindly let us know how it goes.

So, this seems like what I want to do, but the referenced post about how to turn it into a query string leaves me a little confused. I’m not a developer (at least I’m not a good enough developer to follow those instructions). What I really need is the exact string to plug into the tool. It would be awesome if Theme.co created a “widget” to translate that PHP into the query string for us automatically. (Product roadmap idea?)

In the mean time, however, I just used a condition on the looper consumer to hide those posts that don’t match the desired division. I assume this isn’t the best way to go, but it’s working.

1 Like

@ruenel: I’m reading this right now after completing our long other thread. As you can imagine, I support this exactly as @dannikb says: an easier way to create query strings or queries would certainly benefit many users. Thumbs up!

1 Like

@dannikb: I have similar problems as you with filtering my posts.

I just used a condition on the looper consumer to hide those posts that don’t match the desired division

Does these “filtering out” of non-matching posts via consumer-condition work correctly for you? Does it give the output you want and does it suppress every “wrong” post and show all “correct” ones after “wrong” ones again? (I once understood that conditions regulate the display of entire elements and do not work for individual posts? Hence my question…)

1 Like

+1 for an easy query builder

1 Like

@Daryl_Shaw Yes, it appears to be working fine for me at the moment. The looper provider is set to bring in all posts that match the criteria other than the one that is the array (via Query String). Then, the consumer uses the Conditions field to include only those that match the one part that is an array. Happy to show you more detail via screenshots if you want!

1 Like

@dannikb: Yes thanks, would be great to see your settings for this setup!

This is what I want to do:
Per default first showing posts that have a certain category (among others) - let’s call it “featured posts”, directly after that all other posts that are not tagged with this category. The feature category should be read from an ACF that the admin can change easily. No changes should be necessary at the query strings if the feature category shall change.
Then the possibility for visitors to choose a certain category (added as parameter in the URL) so that all posts show up that have that category (among others).

I now solved it by setting up 3 different provider setups with conditions (see Category filtering for blog posts with a "prioritization" for posts with a certain category included)
But I’d like to try again with a simpler setup that only needs one provider / consumer / conditions…

Hi @Daryl_Shaw,
I’m not sure our setup is exactly the same, but my looper provider is a row, and is setup like:


The entire text of the WP Query field is:
post_type=instructors&post_status=publish&order_by=last_name&order=ASC&posts_per_page=-1&meta_key=group&meta_value=admin

Note that I have created a CPT called “instructors”. It has ACFs for last_name and group (among other fields, obviously). The “group” field is setup as a radio button, so there’s a single value that’s going to be selected:

My looper consumer is a column and it setup as:


With the conditions set as:

The first field there was setup by clicking the double arrows on the left and selecting “string.” Then, using the ACF field option to select the right field to filter by. The entire text there is:
{{dc:acf:post_field field="division"}}
(That’s the field that returns an array)

So, for your situation, you might have two rows – one for the featured posts and a second for the “others.” Each of these rows would be setup as a looper provider for the CPT. On the featured posts consumer, you need to give the user somewhere to select the category … maybe an ACF Options field? … and then use the Conditions to include those posts where that “IS” the featured category. Next, on the second row, that consumer’s conditions should use the same string and “IS NOT”. So, in the Conditions box, the first part would be ACF Options Field and the last part would be the string pointing to that value.

Note: If your category selection is multi-select (and therefore an array), it might not work the same way for you. That’s where I got hung up (and the original question on this post) because I wanted to include another meta_key set in my query – for one that returns an array, and it didn’t work like that. I THINK, however, that it will work on Conditions, as mine did.

Hi @dannikb, thanks for the detailed report. Basically, that’s exactly what I did too.
The challenge for me was “featuring” a defined category at the beginning of the listing per default and switching to another listing as soon as the user selects a category himself - and that by using ACF fields and component parameters (thanks to charlie for integrating a fallback option). At the moment it will obviously not work any other way than using several alternative providers for that.

Let’s see how things develop regarding query builder… :wink:

@dannikb,

Thanks for sharing your ideas, we really appreciate it.

@Daryl_Shaw,

Our development team is doing their best to make things easier, we are also looking forward about the enhancement of the query builder.

Cheers!

Guys reading through this thread I can help as the Themeco Legend Josh Donnelly has already created a simple query string generator that work for query string that can be built and copied directly into the looper provider.

https://buildingonwp.com/tools/query-builder/

I’m currently working on a more complex version which I will be adding to my development site as a tool to use online.

1 Like

@Kennyboy7: Ah cool! I didn’t know this yet. I had only found https://crocoblock.com/freemium/tools/wp-query-generator/ but josh’s looks better. This is particularly interesting for me if you can link conditions with includes and excludes… Many thanks so far!

And thumbs up for your query builder project! Charlie and the themeco guys can then integrate that directly into cornerstone :slight_smile:

Hey @Kennyboy7,

Thanks for sharing your ideas, we really appreciate it.

@Daryl_Shaw, I hope this help you to make your work easier.

Cheers!

Holy cow that one is useful.
THANK YOU !!!

You are most welcome, @Thaelys.
We are glad that this thread helps you with your issue.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.