Code snippet to create regular ACF Google Map entry in DB from an ACF Repeater Google Map Sub Field
Budget: $10 – $30 USD
I want to create a snippet which would take repeater field data from a Google Map subfield using the code like from the links below and then create new regular ACF Google Map fields with that looped through data.
Those now multiple extra Google Map fields would be hidden from the front end and only exist in the db to be available for query by mapping plugins.
Using this code for the looping:
```
<?php if( have_rows('locations') ): ?>
<div class="acf-map" data-zoom="16">
<?php while ( have_rows('locations') ) : the_row();
// Load sub field values.
$location = get_sub_field('location');
$title = get_sub_field('description');
$description = get_sub_field('description');
?>
<div class="marker" data-lat="<?php echo esc_attr($location['lat']); ?>" data-lng="<?php echo esc_attr($location['lng']); ?>">
<h3><?php echo esc_html( $title ); ?></h3>
<p><em><?php echo esc_html( $location['address'] ); ?></em></p>
<p><?php echo esc_html( $description ); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
```
I don't need the display part of it. I want that when repeater field data is entered into the Google Map sub field of the repeater field, it creates a new entry as a regular ACF Google Map field entry in the db.
Can anyone help?
I see these code examples for the looping part.
https://www.advancedcustomfields.com/resources/google-map/
https://wpza.net/looping-through-acf-repeater-fields/
ACF Support said: You can use the get_field() function to retrieve the repeater data and use basic array operations to retrieve 'lat' and 'lng' from the location field instead of creating new fields. However, if your mapping solutions require separate ACF fields, I would recommend you take advantage of the acf/save_post callback to update the fields with the data contained in offset 'lat' and 'lng' from the location field.
Those now multiple extra Google Map fields would be hidden from the front end and only exist in the db to be available for query by mapping plugins.
Using this code for the looping:
```
<?php if( have_rows('locations') ): ?>
<div class="acf-map" data-zoom="16">
<?php while ( have_rows('locations') ) : the_row();
// Load sub field values.
$location = get_sub_field('location');
$title = get_sub_field('description');
$description = get_sub_field('description');
?>
<div class="marker" data-lat="<?php echo esc_attr($location['lat']); ?>" data-lng="<?php echo esc_attr($location['lng']); ?>">
<h3><?php echo esc_html( $title ); ?></h3>
<p><em><?php echo esc_html( $location['address'] ); ?></em></p>
<p><?php echo esc_html( $description ); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
```
I don't need the display part of it. I want that when repeater field data is entered into the Google Map sub field of the repeater field, it creates a new entry as a regular ACF Google Map field entry in the db.
Can anyone help?
I see these code examples for the looping part.
https://www.advancedcustomfields.com/resources/google-map/
https://wpza.net/looping-through-acf-repeater-fields/
ACF Support said: You can use the get_field() function to retrieve the repeater data and use basic array operations to retrieve 'lat' and 'lng' from the location field instead of creating new fields. However, if your mapping solutions require separate ACF fields, I would recommend you take advantage of the acf/save_post callback to update the fields with the data contained in offset 'lat' and 'lng' from the location field.