Home Forums ACF PRO ACF Maps trigger on import?


ACF Maps trigger on import?

  • I have a csv of 7000 businesses that i just imported into wordpress.
    Each business has an address field, that i successfully imported into the ACF Google Maps field.
    The problem is the map itself will not find the location without you clicking on the address, hitting enter, then updating the post.
    I can’t do that for 7000 posts, so just wondering if there is a way to do this in bulk? Or even if I re-import the listings, can a pre-set function trigger the maps??

    From the importing plugin, they suggest using a php function to trigger it, but never say what that should be, and to reference ACF.

    Any help would be amazing.


  • What tool did you use to do the importing?

  • Hi john

    We are using WP All Import to import everything. We need to use this plugin because the client wants to update the listings often via csv upload and this plugin allows that, as well as working with ACF fields.

    Just doesn’t trigger the map.

  • I’m familiar with WPAI, I’ve used it. Did support at WPAI point you to any documentation on how to do this? The info on the site says that it’s supposed to support google map fields, did they say anything about why it won’t import a map from an address?

  • No it doesn’t give any info on that. The basic problem I found is that it will import the address, and it shows up in the ACF Google Map field, but since i’m not importing any coordinates, it doesn’t actually make the map itself do anything. The data is just in the address field above the map in the back end.

    So essentially its there, and just needs something to ‘trigger’ it (aka click on the address, hit enter, and it finds it fine).
    But I need something to do this automatically, so we don’t have to go into 7000 posts and do it.

    WPAI support did say this:

    currently there is no geocoding feature included on the import of ACF Google Map fields. You would need to directly import the latitude and longitued values if you want the map to be automatically rendered without any manual trigger. Entering just an address is not enough to render a map.

    You could automate this by creating a custom php function that calls on the geocoding API of your choice (most commonly the Google Maps API) and get the lat/long details based on your street address.

    But i have no idea how to event start looking into that.

  • I’ve created filters for WPAI in the past to do importing of data, but I have not done anything like this either. You’d need to set up an API key and create a PHP script to connect and get the information you need and then update the field. There is information here

    But I think in the end that would fail when you tried to import 7000 addresses. I really think it would time out, or at some point google would refuse to look up any more addresses. The Google Map API has some limits on how many you can look up that are detailed in the doc I linked to above. If you pay for the service the limits are higher, 100K/day and 10/second, but I still think you’re import would end up timing out with the need to connect to google for every record.

    Just looking through the ACF code, the reason that it’s not triggered is that ACF does the lookup using JavaScript when you enter an address and what you need for the import is a PHP function that does the same thing.

    My suggestion would be to have fields in the spreadsheet for the lng and lat values so that you could build a filter for WPAI and do the import without needing to do the lookup on every address during the import. The problem there is that you have 7000 of them and someone is going to have to manually look up all those addresses…. not a job I’d enjoy doing. But it would only be a pita once and then it would just be a matter of adding the new ones.

  • Hi John

    Ok thanks for the info. We’re going to try a few things and I’ll reply with what finally works!

  • Don’t know if you’re going to try to use the maps api or put the data directly in the spreadsheet. If you going to go the manual path and then build a filter for WPAI.

    1) Import the lng, lat and address into custom fields unrelated to ACF. This can be done in the first section of the import template

    2) In the import filter, get the values from the fields you imported above get_post_meta() and build the array for ACF.

    3) use update_post_meta() to insert the value into the ACF field

    4) Don’t forget to use update_post_meta() to add the ACF key field. Each ACF field has a corresponding field in the database for the field key.

    Even if you use the google API you’ll need to do 3 & 4

  • I’ve created a PHP script for this purpose as I ran into the same issue. John’s instructions of step #4 doesn’t apply, as it expects the ACF field already exists – and just doesn’t have lat/lng.

    Here’s the link to the other forum thread:

  • Hopefully this helps others out in the future. This is for WP ALl Import, it uses Google GEO Location API.

    // Get location data from Google
    function parse_address_google( $address = '' ) {
        if( empty( $address ) ) {
        $geolocate_api_key = 'XXXX';
        $address = urlencode( $address );
        $request = wp_remote_get("$address&key=$geolocate_api_key");
        $json = wp_remote_retrieve_body( $request );
        $data = json_decode( $json );
        if ( !$data ) {
    		// ERROR! Google Maps returned an invalid response, expected JSON data
    	if ( isset($data->{'error_message'}) ) {
    		// ERROR! Google Maps API returned an error
    	if ( empty($data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}) || empty($data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}) ) {
    		// ERROR! Latitude/Longitude could not be found
        $array = json_decode( $json, true );
        $result = $array['results'][0];
        $location = array();
        // street_number, street_name, city, state, post_code and country
        foreach ($result['address_components'] as $component) {
            switch ($component['types']) {
              case in_array('street_number', $component['types']):
                $location['street_number'] = $component['long_name'];
              case in_array('route', $component['types']):
                $location['street_name'] = $component['long_name'];
              case in_array('sublocality', $component['types']):
                $location['sublocality'] = $component['long_name'];
              case in_array('locality', $component['types']):
                $location['city'] = $component['long_name'];
              case in_array('administrative_area_level_2', $component['types']):
                $location['region'] = $component['long_name'];
              case in_array('administrative_area_level_1', $component['types']):
                $location['state'] = $component['long_name'];
                $location['state_short'] = $component['short_name'];
              case in_array('postal_code', $component['types']):
                $location['postal_code'] = $component['long_name'];
              case in_array('country', $component['types']):
                $location['country'] = $component['long_name'];
                $location['country_short'] = $component['short_name'];
          $location['lat'] = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    	  $location['lng'] = $data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
          return $location;
    // run on import
    function _s_acf_update_map_field( $id ) {
        // Get ACF map field    
        $field = get_field( 'field_5eb49ed952662', $id );
        if( empty( $field['address'] ) ) {
        $location = parse_address_google( $field['address'] );
        $args = wp_parse_args( $location, $field );
        update_field( 'field_5eb49ed952662', $args, $id );
    add_action( 'pmxi_saved_post', '_s_acf_update_map_field', 10, 1 );
  • Hey @kylerumble,

    Thanks for that! Can you please share some details more? I add this code in functions and call it from WP all Import?

  • Thanks @kylerumble, works great!
    For those who want to use this snippet, do not forget to disable the restriction (on domains or IPs) of the Google API key if you have one, otherwise it won’t work.

  • Plese @kylerumble can you tell me how to use it? I’m not able. I entered it in function.php and if I use wp all import to import address, I must press “Enter” to activate the address. How can I solve? Thank you.

  • @mercurio Place the code at the bottom of your import template in the Function Editor. More info here:

    Don’t forget to update your field id, api key, and value for $address (line 3) which should just be the column name as shown in the wp all import list of data on the right side of your import template editing screen.

    The acf map field with the Google Maps API generates lng and lat when an address is typed in and you click on it. So if you do not have lat and lng to import, you can use the function @kylerumble provided to accomplish this. I had some luck with this. if any problems occur check the value on line 3. That seems to be the only thing I was not clear on from @kylerumble

    Thank you all for contributing! I hope that helps someone!

  • I’m using WordPress Ultimate CSV Import ( and I’m having the same problem with the address when importing.

    “The problem is the map itself will not find the location without you clicking on the address, hitting enter, then updating the post.”

    Is there a solution for this Plugin too? 🙂

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

The topic ‘ACF Maps trigger on import?’ is closed to new replies.