Home › Forums › ACF PRO › WP_query using meta_query for an ACF checkbox field › Reply To: WP_query using meta_query for an ACF checkbox field
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),
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.