Support

Account

Home Forums General Issues perform a meta query on google maps array

Solved

perform a meta query on google maps array

    • rudtek

    • March 19, 2020 at 6:39 am

    I’m attepting to do a meta query on a google maps field. The map field is called “full address”.
    Here is my start which works.

    if(isset($_GET[‘city’])) {
    $city = sanitize_text_field( $_GET[‘city’] );
    $meta_query[] = array(
    ‘key’ => ‘full_address’,
    ‘value’ => $city,
    ‘compare’ => ‘LIKE’
    );
    }

    The problem is that if I’m querying a city “Boise” but there is an address in my db that is: 123 boise road, medford, oregon.

    it shows up as well.

    So I’d like to instead of using the whole field as the key for the meta query to be able to use ‘full_address:city’. Is this possible?

  • The field is stored as a serialized array. Put double quotes around the value to get the exact match.

    
    value’ => '"'.$city.'"',
    
    • rudtek

    • March 24, 2020 at 2:01 am

    Thanks John! Can you explain why this works? Or point me in a research direction?

  • The data in the database is serialized

    For example, an array with Boise in the city might look like this

    
    a:2:{s:4:"city";s:5:"Boise";s:7:"address";s:15:"123 Main Street";}
    

    By adding the quotes around the value we’re looking for matches this city

    On the other hand, with Boise in the street it might look something like

    
    a:2:{s:4:"city";s:13:"Somewheretown";s:7:"address";s:16:"123 Boise Street";}
    

    and it does not match because there are no quotes around Boise in the DB value.

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

You must be logged in to reply to this topic.

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.