i am using ACF pro combined with a meta_query, and am struggling to get a couple of the keys to work properly, i am using ACF for a number of custom user preferences, lets focus on 2 of them, “property_type” and “number_of_bedrooms”
property_type is a list of checkboxes, so the user can select more than one at a time, number_of_bedrooms is a dropdown select, so the user can only choose one option for this
i have the following meta query:
Array
(
[meta_query] => Array
(
[relation] => AND
[0] => Array
(
[key] => property_type
[value] => Apartment
[compare] => LIKE
)
[1] => Array
(
[key] => number_of_bedrooms
[value] => 3
[compare] => =
[type] => numeric
)
)
)
for the “property_type” if i use the
'compare' => 'LIKE'
the query will return the correct results, but if i use
'compare' => '='
the query will return empty
if i do the same thing on the “number_of_bedrooms”, the “=” works as i it should
when i look at the user meta data i see the property type info is being stored inside another array like so:
[property_type] => Array
(
[0] => Array
(
[0] => Apartment
[1] => Chalet
[2] => Land
[3] => Renovation
[4] => Leaseback
[5] => Business
[6] => Commercial
[7] => Villa
[8] => Hotel
)
)
as opposed to the number of bedrooms which looks like this:
[number_of_bedrooms] => Array
(
[0] => 1
)
i think this is why the ‘compare’ => ‘=’ isnt returning the values i am expecting on the property_type, am i missing something glaringly obvious here? how do i use the meta_query to query a sub array??
thanks for any guidance on this, im sure/hoping im missing something really obvious!
kind regards
Tommy
ok, so i think i might of found a solution, adding some “” around the searched value makes it behave as an equals, as opposed to a like, that way, if i were to create a new property with another property type name inside it, lets say “Apartmentslongword”, it doesnt get returned.
hope this helps someone else who might of run into this problem
if i have this wrong, please can you explain below,
THANKS!
@bowersinit glad you worked it out.
The difference between the two fields is the same anywhere that you can choose one value or multiple values. Any ACF field that allows multiple values to be selected stores that values as a serialized array instead of a single value. The best explanation of querying values that store serialized arrays in on this page http://www.advancedcustomfields.com/resources/querying-relationship-fields/
You must be logged in to reply to this topic.
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.