Support

Account

Home Forums ACF PRO meta_query with ACF

Solving

meta_query with ACF

  • 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/

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘meta_query with ACF’ is closed to new replies.