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 ) ) {
return;
}
$geolocate_api_key = 'XXXX';
$address = urlencode( $address );
$request = wp_remote_get("https://maps.googleapis.com/maps/api/geocode/json?address=$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
return;
}
if ( isset($data->{'error_message'}) ) {
// ERROR! Google Maps API returned an error
return;
}
if ( empty($data->{'results'}[0]->{'geometry'}->{'location'}->{'lat'}) || empty($data->{'results'}[0]->{'geometry'}->{'location'}->{'lng'}) ) {
// ERROR! Latitude/Longitude could not be found
return;
}
$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'];
break;
case in_array('route', $component['types']):
$location['street_name'] = $component['long_name'];
break;
case in_array('sublocality', $component['types']):
$location['sublocality'] = $component['long_name'];
break;
case in_array('locality', $component['types']):
$location['city'] = $component['long_name'];
break;
case in_array('administrative_area_level_2', $component['types']):
$location['region'] = $component['long_name'];
break;
case in_array('administrative_area_level_1', $component['types']):
$location['state'] = $component['long_name'];
$location['state_short'] = $component['short_name'];
break;
case in_array('postal_code', $component['types']):
$location['postal_code'] = $component['long_name'];
break;
case in_array('country', $component['types']):
$location['country'] = $component['long_name'];
$location['country_short'] = $component['short_name'];
break;
}
}
$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'] ) ) {
return;
}
$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 );
I’m having similar issues where repeater fields are not working in a widget. I creates a repeater called logos, and places it in a footer widget. It only contains 3 small logos and all fields have short names.
It works on all pages, posts, archives etc….
But then when you do a search, they are missing from the search results page.
How would I query widget fields directly like you can do posts with get_post_meta()?
Thanks.
I only wish I could afford to buy a ticket from Vancouver BC Canada to WordCamp Europe. 😛
Sadly it’s not in this years budget.
Jonathon,
Thanks so much for the help. I ended up just making the end date mandatory. This turned into to much work so I just went that route.
I’ll try your last suggestion still, but if I don’t use it I still wanted to just say thanks.
Send me your paypal email and I’ll buy you a beer.
Got it to work using this code here:
function my_acf_load_field( $value, $post_id, $field )
{
$event_start = get_field_object('field_5217ac66687dc', $post_id);
$event_end = get_field_object('field_524b4ca5ff418', $post_id);
if( empty( $event_end['value'] )):
$value = $event_start['value'];
endif;
return $value;
}
//add_filter('acf/update_value/key=field_524b4ca5ff418', 'my_acf_load_field', 10, 3);
Problem is, if you save it duplicates it if empt, then if you clear it and hit save again, it is blank till you hit save a second time.
If anyone can solve this for me I’d love to buy them a few beers.
I’m not sure if it makes any difference or not that its a date picker field.
Sadly that didn’t work.
I checked the original method inside the acf.php file and tried this as well with no results.
function my_acf_save_post( $post_id )
{
// load from post
if( !isset($_POST['fields']) )
{
return false;
}
// loop through and save
if( $_POST['fields'] )
{
$fields = $_POST['fields'];
// concert details
// event start date
//$fields['field_5217ac66687dc']
// event end date
//$fields['field_524b4ca5ff418']
if( empty( $_POST['fields']['field_524b4ca5ff418'] ) )
$_POST['fields']['field_524b4ca5ff418'] = $fields['field_5217ac66687dc'];
// calendar details
// event start date
//$fields['field_524b4c06af2a2']
// event end date
//$fields['field_524b4c3aaf2a4']
if( empty( $_POST['fields']['field_524b4c3aaf2a4'] ) )
$_POST['fields']['field_524b4c3aaf2a4'] = $fields['field_524b4c06af2a2'];
foreach( $_POST['fields'] as $key => $value )
{
// parse types
// - caused issues with saving numbers (0 were removed)
//$value = apply_filters('acf/parse_types', $value);
// get field
$field = apply_filters('acf/load_field', false, $key );
// update field
do_action('acf/update_value', $value, $post_id, $field );
}
// foreach($fields as $key => $value)
}
// if($fields)
return true;
}
I’m super stumped, which sucks sine I write code all day long.
Did that already. It prints out an array of fields. That’s actually how I got the field keys.
Maybe I’m wrong about what the hook does and how it does it. Is the $fields array saved, or is the $_POST array saved.
All I know is when I hit update, the end date field is still blank.
K, sorry, I must be missing somehting. Here’s my code that doesn’t seem to work.
function my_acf_save_post( $post_id )
{
// vars
$fields = false;
// load from post
if( isset($_POST['fields']) )
{
$fields = $_POST['fields'];
// concert details
// event start date
//$fields['field_5217ac66687dc']
// event end date
//$fields['field_524b4ca5ff418']
if( empty( $fields['field_524b4ca5ff418'] ) )
$fields['field_524b4ca5ff418'] = $fields['field_5217ac66687dc'];
// calendar details
// event start date
//$fields['field_524b4c06af2a2']
// event end date
//$fields['field_524b4c3aaf2a4']
if( empty( $fields['field_524b4c3aaf2a4'] ) )
$fields['field_524b4c3aaf2a4'] = $fields['field_524b4c06af2a2'];
}
// ...
}
// run before ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_acf_save_post', 1);
Jonathan, thanks for the response. I guess I just need to manipulate the field keys then and ACF does all the heavy lifting in the back end then.
I was just worried that the field keys were dynamic and would change. That’s why I was looking for the actual key names created such as mom_event_date”
I’ll give that a try and see how it works.
I’m getting the same error. I have no idea why its happening.
It just doesn’t want to save the repeater fields at all.
Found the issue:
http://wordpress.org/plugins/simple-mail-address-encoder/
Do not use this plugin, it will break all your repeater fields.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.