Tell us what you are trying to do?
Building a directory site and want to be able to sort results by proximity. In order to do this the location needs to be stored as longitude, latitude. Google Maps already has this info but I'm not sure how to get it and insert into the field. It's in a different array from the rest of the address data. hidden link
Is there any documentation that you are following?
https://toolset.com/forums/topic/extracting-address-data-from-maps-field/
https://toolset.com/forums/topic/split-extracting-address-data-from-maps-field-and-saving-as-taxonomy-term/
Is there a similar example that we can see?
The code from the above links is working well. Need to extract another datapoint in the form of lat,long.
What is the link to your site?
hidden link
This is the missing piece for this site. With this we will be able to sort by proximity.
Thank you!
Jen
I managed to get this working for front end edit form by adapting the code from extracting the state to taxonomy.
This works on edit but not create. Perhaps because I used a shortcode to get lat/long and that value is not yet available on first save. Not sure how else to get the lat/long as that data is in a different array from the address components. I'm sure there is a better way.
// add long/lat
add_action( 'save_post', 'custom_process_longlat_save_post', 10,3 );
function custom_process_longlat_save_post( $post_id, $post, $update ) {
if ( ('listing' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) {
return;
}
$post_id = $post->ID;
if(!empty($_POST['wpcf-street-address'])) {
$street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
custom_process_new_longlat($post_id, $street_location);
}
if(!empty($_POST['wpcf']['street-address'])) {
$street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
custom_process_new_longlat($post_id, $street_location);
}
}
function custom_process_new_longlat($post_id, $street_location) {
update_post_meta( $post_id, 'wpcf-location', $street_location );
}
Hi,
Your understanding is correct and the use of shortcode to get the custom field value from the table, it too early for new posts, as the value has not been saved in the table yet.
Instead of getting the values from the custom fields table, you can use the superglobal "$_POST".
Example:
// add long/lat
add_action( 'save_post', 'custom_process_longlat_save_post', 10,3 );
function custom_process_longlat_save_post( $post_id, $post, $update ) {
if ( ('listing' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) {
return;
}
$post_id = $post->ID;
if(!empty($_POST['wpcf-street-address'])) {
// removed line: $street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
$street_location = $_POST['toolset-extended-form-wpcf-street-address']['latitude'].', '.$_POST['toolset-extended-form-wpcf-street-address']['longitude'];
custom_process_new_longlat($post_id, $street_location);
}
if(!empty($_POST['wpcf']['street-address'])) {
// removed line: $street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
// removed line: custom_process_new_longlat($post_id, $street_location);
$street_location = $_POST['toolset-extended-form-wpcf']['street-address']['latitude'].', '.$_POST['toolset-extended-form-wpcf']['street-address']['longitude'];
$_POST['wpcf']['post-location'] = $street_location;
}
}
function custom_process_new_longlat($post_id, $street_location) {
update_post_meta( $post_id, 'wpcf-location', $street_location );
}
This should do the trick, for new and/or existing posts.
regards,
Waqar