Skip Navigation

[Resolved] Extracting Latitude and Longitude to a custom field on post creation

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Waqar 2 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2402137

When I manually create a new custom post I have a script running that extracts the lat/long from the map address and saves it in a field called business-location. Here is the code for this action:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

error_log("code is running...");
add_action( 'save_post', 'ts_save_coordinates',100,3);
function ts_save_coordinates( $post_ID, $post, $update){
	if ( $post->post_type == 'after-death-service' ) {
   	if ( isset($_REQUEST['toolset-extended-form-wpcf']['business-address']) ){
      	error_log("condition met");
        $lat = $_REQUEST['toolset-extended-form-wpcf']['business-address']['latitude'];
        $lon = $_REQUEST['toolset-extended-form-wpcf']['business-address']['longitude'];
     
        $coords = "$lat,$lon";
      
 		error_log("coords: $coords");
        update_post_meta( $post_ID, 'wpcf-business-location', $coords );
    }
    }
}

BUT

When I bulk upload multiple custom posts the lat / long field is not being updated automatically. I believe this is because the address value has not been saved in the table yet similar to this post: https://toolset.com/forums/topic/extracting-longitude-latitude-from-maps-field/#post-2257137

This support post recommends "Instead of getting the values from the custom fields table, you can use the superglobal "$_POST"." But I am unsure how to exactly do that with my code.

Currently I have to individually edit and update each bulk imported post for the lat/long to be extracted & saved in the field business-location

Please help me edit my code so that upon import the lat/long field will be updated automatically. Thank you

#2402649

Hi,

Thank you for contacting us and I'd be happy to assist.

In what format does your import file contains the lat/long coordinates?

If you could set the import file to hold the "wpcf-business-location" field's lat/long value in the format {lat,long} ( e.g. {40.6971494,-74.2598671} ), the custom field value will be set during the import process and you won't have to add any custom code for that or even manually update those posts, later.

If editing the import file's data is not an option, you can share more information about the format of that file and the import process and I'll be in a better position to suggest the next steps, accordingly.

regards,
Waqar

#2402807

Thank you for the reply. My import file does not contain the lat/long coordinates. It only contains a location's address.

Upon post creation the address is used to extract the lat/long using google maps with the code in my original post. When I manually create a single post the code works as expected and the lat/long are extracted and saved. But upon a bulk import the lat/long is not being extracted.

#2403609

Thank you for sharing this.

If your import file contains the location's address, you don't have to extract the lat/long through a custom code.

During the import, you can store the human-readable address in the custom field 'wpcf-business-location' and then once the import is completed, use the "Check for missing cache entries" button at WP Admin -> Toolset -> Settings -> Maps -> Cached data.

It will automatically look for all the human-readable addresses in the address type fields, Geocode their lat/long, and then save them in the Toolset Map's cache, if they don't already exist.

#2403619

Ok but my reason for extracting the lat/long into their own custom field is because I am using that field on the front end of my website to calculate the time of sunset at each post's location. So for that code to operate correctly I need the lat/long in a specific custom field I created called business-location.

#2403645

What you suggested in this post --> https://toolset.com/forums/topic/extracting-longitude-latitude-from-maps-field/#post-2257137 looks almost identical to the problem I'm having. When I bulk import my posts I want the lat/long extracted from the address and placed in the custom field business-location

#2404249

> Ok but my reason for extracting the lat/long into their own custom field is because
> I am using that field on the front end of my website to calculate the time of sunset at each post's location.
> So for that code to operate correctly I need the lat/long in a specific custom field I created called business-location.

- I would not recommend using a separate custom field "business-location" just to store the lat/long data of the address that is already stored in the "business-address" field. It will become complicated to keep the data between these two fields in sync, in case one of them is changed.

Instead of calling the lat/long from a separate custom field, you can call them from the Toolset address type field directly in your custom code for the sunset time calculation:
https://toolset.com/documentation/customizing-sites-using-php/functions/#address

For example:


global $post;
	
$address_coordinates = types_render_field( "business-address", array('item' => $post->ID , 'format' => 'FIELD_LATITUDE,FIELD_LONGITUDE') );

I've also tested the import process of the "WP Ultimate CSV Importer" plugin on my website and noticed that when I imported the human-readable address in the Toolset address type custom field, I didn't even have to use the "Check for missing cache entries" button. The relevant geocoded lat/long coordinates were automatically updated in the Toolset's map cache, after the import.

On the other hand, if you'd still prefer to store the lat/long in a separate custom field, you'll need to consult the "WP Ultimate CSV Importer" plugin's support and documentation, to see if it offers any hook to run some custom code after a successful import. If it does, you can include a custom function that cycles through all the posts in the target post type, and for the posts where the value of the "business-location" field is empty, it can get it from the "business-address" field and update it in the "business-location" field.

An example of such a function is available in this reply:
https://toolset.com/forums/topic/make-custom-post-types-sticky/#post-2372715

> What you suggested in this post
> https://toolset.com/forums/topic/extracting-longitude-latitude-from-maps-field/#post-2257137
> looks almost identical to the problem I'm having. When I bulk import my posts I want the lat/long
> extracted from the address and placed in the custom field business-location

- The requirement in that other ticket is different because it covers the cases of post creation/editing through the post edit screen in the admin area or the front-end Toolset Forms. The same data is not available when the "save_post" hook is used for the bulk import of posts using any importer plugin.