Problem: I have two address fields, one in a parent post type and one in a child post type. When a new child post is created, I would like to calculate the distance between the two addresses and save that distance in another custom field in the child post type.
Solution: You can use the cred_submit_complete hook to trigger custom code with the shortcode toolset-maps-distance-value to calculate the distance between two addresses. Use the update_post_meta function to store that distance in another custom field.
add_action('cred_submit_complete', 'tssupp_calc_parent_child_distance',10,2); function tssupp_calc_parent_child_distance($post_id, $form_data) { $forms = array(123, 456); $relationship_slug = 'book-chapter'; $parent_address_slug = 'book-address-1'; $child_address_slug = 'chapter-address-1'; $child_distance_slug = 'chapter-single-line-1'; $unit = 'km'; $decimals = 2; // if a specific form if ( in_array( $form_data['id'], $forms ) ) { $related_post = toolset_get_related_post( $post_id, $relationship_slug, 'parent' ); if( $related_post ) { $child_location = get_post_meta( $post_id, 'wpcf-'.$child_address_slug, true ); $parent_location = get_post_meta( $related_post, 'wpcf-'.$parent_address_slug, true ); if( $child_location && $parent_location ) { $distance = do_shortcode("[toolset-maps-distance-value location='" . $child_location . "' postmeta='wpcf-". $parent_address_slug . "' postmeta_id='" . $related_post . "' unit='" . $unit . "' decimals='" . $decimals . "']"); update_post_meta( $post_id, 'wpcf-'.$child_distance_slug, $distance ); } } } }
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#toolset-maps-distance-value
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 |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 5 replies, has 2 voices.
Last updated by 3 years, 5 months ago.
Assisted by: Christian Cox.