Thanks for writing back.
On my test website, I was able to achieve this, using a custom function attached to the 'wp_insert_post' hook.
( ref: https://developer.wordpress.org/reference/hooks/wp_insert_post/ )
This approach is much more reliable compared to the script method because it will keep the latitude and longitude values in separate custom fields in sync, no matter how a post is created or updated.
1. For the custom post type 'Books' (slug: book ), I added these custom fields:
a). Address type custom field with slug 'book-address'.
b). Single line type custom field with slug 'book-latitude'.
c). Single line type custom field with slug 'book-longitude'.
2. Next, I added the following custom function, which copies the lat/lon values from the address type custom field to the separate single line type custom fields, every time a 'Book' post is created or updated:
function custom_update_address_fields($post_id, $post, $update) {
// skip if post revision
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// skip if post type is not the target post type 'book'
if ( get_post_type( $post_id ) != 'book' ) {
return;
}
// get the lat/lon values from the address type custom field 'book-address'
$address_field_val = types_render_field("book-address", array( 'item' => $post_id, 'format' => 'FIELD_LATITUDE,FIELD_LONGITUDE' ));
// If the address field value is not empty
if(!empty($address_field_val)) {
// Explode the lat and lon values
$address_field_val_arr = explode(',', $address_field_val);
// Update the lat and lon values in the separate custom fields
update_post_meta( $post_id, 'wpcf-book-latitude', $address_field_val_arr[0] );
update_post_meta( $post_id, 'wpcf-book-longitude', $address_field_val_arr[1] );
}
}
add_action( 'wp_insert_post', 'custom_update_address_fields', 9999, 3 );
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
Please adjust the custom post type and custom field slugs in the code, as used on your website.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/