Support

Account

Home Forums ACF PRO Saving Google Map lat/lon data as post_meta

Solved

Saving Google Map lat/lon data as post_meta

  • Hi- I’m trying to store the Google Map coordinate data into its own separate field so I can query it more easily, but for some reason, the act/update_value/name hook is mucking with the saving of the actual field data.

    Here’s my function:

    function rhm_update_lng_and_lat( $value, $post_id, $field ) {
    
        update_post_meta( $post_id, 'loc_lat', $value['lat'] );
        update_post_meta( $post_id, 'loc_lng', $value['lng'] );
    
    }
    add_action('acf/update_value/name=map_location', 'rhm_update_lng_and_lat', 10, 3  ); 
    

    When I find the location in the map, and hit Update, the coordinates get saved to their new post_meta locations, but the serialized map data doesn’t get saved. If I remove the action call, the map data does get saved again.

    I had originally been using /acf/update_value/ without the name flag (but checking the field type and field name), and on update, the coordinate data was getting saved, but all other ACF data for this post was getting wiped out.

    That function is here:

    add_action('acf/update_value', 'rhm_update_lng_and_lat', 99, 3  ); 
    
    function rhm_update_lng_and_lat( $value, $post_id, $field ) {
    
        if(	'google_map' === $field['type'] && 'map_location' === $field['name'] ) {	
    
            update_post_meta( $post_id, 'loc_lat', $value['lat'] );
            update_post_meta( $post_id, 'loc_lng', $value['lng'] );
    
        }
    
    }
    

    What am I doing wrong, or is this a bug in the update_value hook?

  • Update.. It looks like the code I’m using is also blowing out other loc_lat and loc_lng post_meta on save, so if I update one location, the others all get wiped out πŸ™

  • So, I wound up using save_post vs acf/update_value, and that seems to work.

    Here’s my final working function for anyone else that’s looking to extract lat/lon out of the map field:

    function rhm_update_latlon($post_id, $post, $update) {
    
    	$map = get_post_meta($post_id, 'map_location', true);
    
    	if (!empty($map)) {
    	    update_post_meta( $post_id, 'loc_lat', $map['lat'] );
    	    update_post_meta( $post_id, 'loc_lng', $map['lng'] );
    	}
    
    }
    add_action('save_post', 'rhm_update_latlon', 90, 3);
    

    That said, it would still be great to see the map_location data stored separately as individual fields (even better would be to break out the individual address components) so they could all be queried and extracted as needed.

  • Thank you for this code

  • The problem with your first example was that you were not returning $value. Since it’s a filter you need to return something, either the original value or an updated one.

    
    function rhm_update_lng_and_lat( $value, $post_id, $field ) {
    
        update_post_meta( $post_id, 'loc_lat', $value['lat'] );
        update_post_meta( $post_id, 'loc_lng', $value['lng'] );
        return $value;
    }
    add_action('acf/update_value/name=map_location', 'rhm_update_lng_and_lat', 10, 3  );
    
  • Hi,

    I tried both solutions, Rob’s one works, but John’s one doesn’t.
    I thought it was a problem in rendering/loading the google_map field/value,
    so I’ve tried update_value on a basic ACF text field, passing its $value to another ACF field, but nothing happens.
    I’ve tried overriding the returned $value as in docs usage example and it fires only when I save the post (publish or update – so I don’t get the difference with save_post action).
    As far as it regards:

    do something else to the $post object via the $post_id

    neither ACF update_field nor WP update_post_meta seems to work.

    So, hooking WP save_post does the job, but I wonder if there’s a way to dynamically update ACF fields while updating other ACF field value.
    Cheers

  • How do you query your individually stored coordinates? Just by exact value? Or also to, in some way, find nearby locations?

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

You must be logged in to reply to this topic.