Support

Account

Home Forums ACF PRO Query posts by ACF google maps value

Helping

Query posts by ACF google maps value

  • Hello,

    I’ve just ran into a challenge and cannot solve it. I have a CPT, called ‘places’.
    I use google map to store the address of places.

    I want to implement a custom search for the places, where user can select city.

    Is it possible to:

    a.) List all cities where I have places
    b.) Get all places from a specific city

    Thank you for your help & time

  • The google map field stores a serialized array that looks something like this

    
    a:3:{s:7:"address";s:42:"XXXX XXXXXX Road, Pxxxx, NY, United States";s:3:"lat";s:9:"42.979788";s:3:"lng";s:18:"-75.29690099999999";}
    

    It would be extremely difficult to list all of the cities. To do this you would need to

    1. Get all of the posts
    2. Loop through the posts and get the field value
    3. Extract the city from the first array element
    4. Store the unique cities in an array
    5. Sort the array

    Searching for a specific city would require a LIKE query similar to doing a query on a relationship field https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    The easiest solution would be to provide a city field that can be more easily searched.

    The next best solution would be to extract the city from the map field when the post is saved and then store it in a different postmeta field to search on that. There is an example of the basics of doing this in this topic https://support.advancedcustomfields.com/forums/topic/update-post_meta-with-acf-date-value/. It does not deal with map fields and you’d have to figure out how to extract the city from it. To be honest I’m not sure how that could be done exactly.

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

The topic ‘Query posts by ACF google maps value’ is closed to new replies.