Tagged: automatic, bulk update, gmw_pt_update_location, location
- This topic has 2 replies, 2 voices, and was last updated 3 years, 6 months ago by
advertiseit.
-
AuthorPosts
-
January 5, 2016 at 8:00 am #49880
aaahollister
MemberI am building a system where a user can import a CSV file with over 1000 ‘Doctors’ and their addresses into WordPress using the All Import Pro plugin, the import creates a post for each Doctor and fills in various meta which includes their addresses, but unfortunately not Lat and Long values.
I am trying to use the gmw_pt_update_location function to set the location of each of these posts automatically when the posts are created, using a combination of their street address, city, and country.
The update function works if I go into one of the posts and then refresh the page, however I don’t seem to be able to get it to update the locations for all posts, and I’m really looking for something that can run on each post when it is created to update the location per post. I’ve tried various different hooks and I’ve also tried wrapping the below function in a WP_Query looping through all the posts too.
Is there a way I can get this function to run, either when posts are created or in a bulk way after the import, and successfully set the locations for each post?
Thanks,
Adam H123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960add_action( 'updated_post_meta', 'gmw_update_post_type_post_location', 10, 4 );//rename your custom function as you wish ( or leave it as is ).function gmw_update_post_type_post_location( $meta_id, $post_id, $meta_key, $meta_value ) {$post_id = get_the_id();$meta_key = '_pwl_doc_meta_postal_code';$meta_id = get_mid_by_key( $post_id, $meta_key );$meta_value = get_post_meta( $post_id, $meta_key, $single=true );$street_key = '_pwl_doc_meta_address_01';$street_value = get_post_meta( $post_id, $street_key, $single=true );$city_key = '_pwl_doc_meta_town_city';$city_value = get_post_meta( $post_id, $city_key, $single=true );$country_key = '_pwl_doc_meta_country';$country_value = get_post_meta( $post_id, $country_key, $single=true );// If this isn't a 'doctor' post, do nothing.$slug = 'doctors';if ( $slug != get_post_type( $post_id ) ) {return;}// Return if it's a post revisionif ( false !== wp_is_post_revision( $post_id ) )return;// Check autosaveif ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {return;}// Check if user can edit postif ( !current_user_can( 'edit_post', $post_id ) )return;// Include the update location file fileinclude_once( GMW_PT_PATH .'includes/gmw-pt-update-location.php' );// Make sure the file included and the function existsif ( !function_exists( 'gmw_pt_update_location' ) ) {return;}$address_value = $street_value .', '. $city_value .', '. $country_value;// Create the array that will pass to the function$args = array('post_id' => $post_id, // Post Id of the post'address' => $address_value, // the address we pull from the custom field above);//run the update location functiongmw_pt_update_location( $args );}January 7, 2016 at 7:11 am #49919aaahollister
MemberManaged to figure it out, turned out to be using the wrong hook from the WP All Import Pro plugin. For anyone trying to achieve an integration like this between these two plugins, if you want your code to run on each post as it is being created from the CSV import, you need to use the gmw_pt_update_location function on the pmxi_saved_post action.
Thanks for such a useful plugin!
December 13, 2018 at 9:10 am #58365advertiseit
MemberHi @aaahollister,
Thanks for posting this. I know this is old but would you be able to post your corrected function. It would help immensely.
Thanks,
Jason -
AuthorPosts
The topic ‘Using gmw_pt_update_location to set locations only works on manual refresh’ is closed to new replies.