Hello,
i have users with certains characteristics (registered as ACF checkboxes).
I’m trying to retrieve terms from “groupe” taxonomy that match those characteristics (checkboxes too, in a group field called “ciblages”).
To do so, i’m trying to run the following with get_terms :
[taxonomy] => groupe
[hide_empty] => 0
[fields] => ids
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[key] => ciblages_ciblage_secteur
[value] => Array
(
[0] => 373
[1] => 374
)
[compare] => LIKE
)
)
)
Problem is :
1) it works when value is a single id ( ‘value’ => 374 )
2) it doesn’t work when value is an array.
This exact query returns EVERY term from “groupe” taxonomy, not just those that match.
And when I try and use “IN” instead of “LIKE”, it returns nothing.
I suspect it’s because ACF stores data as serialized objects, so I can’t really search anything ACF Checkboxes in a meta_query ?
Any idea about how I could achieve what i’m trying to do ?
Thanks a lot in advance 🙂
Ok, found a solution using
https://support.advancedcustomfields.com/forums/topic/wp_query-using-meta_query-for-an-acf-checkbox-field/
The following works :
[meta_query] => Array
(
[relation] => OR
[0] => Array
(
[key] => ciblages_ciblage_secteur
[value] => 373
[compare] => LIKE
)
[1] => Array
(
[key] => ciblages_ciblage_secteur
[value] => 374
[compare] => LIKE
)
)
)
I’m pasting a note from original solution:
Given answer has potential to give false positives.
E.g. you’re after posts with IDs 528 and 76, but there are also posts with IDs 5287, 4528, 765 and 2769 which you don’t intent to get, but they might happen to match the rest of the query.
The same is true to any values, since matching “apple” will give you false positive on “apple_pie” or “applejuice” etc.
Internally arrays are serialized and look like
a:4:{i:0;s:3:"528";i:1;s:5:"15289";i:2;s:5:"apple";i:3;s:9:"apple_pie";}
note double quotes around values – which you can use to make sure that you’re matching exactly what you want
To safely query for values in them, just include quotation around value you’re after:
$meta_query[] = array(
'key' => 'checkbox',
'value' => sprintf('"%s"', $item),
'compare' => 'LIKE',
);
You can also add colon before and semicolon after to explicitly telegraph your intention to query serialized value:
'value' => sprintf(':"%s";', $item),
I have similar sitauation but with labels. Query works fine if asigned value ist just one. If I asigne few values to to one custom filed, ACF returning me an array of values and i cant get to them. The query returning nothing. Any advices how to fix it?
`[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[0] => Array
(
[key] => technika_wykonania
[value] => Array
(
[0] => ciecie-laserowe
)
[compare] => IN
)
)
)
)
You cannot query values in checkboxes using IN. See the this page, section 3. Multiple custom field values (array based values)
The topic ‘Meta query with array as values’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.