Thanks for writing back.
To keep the location custom fields between the parent and child posts in sync, you can use some custom code.
For example, to make sure that when a relationship connection is created between a parent and child post, the parent post's address field value is copied to the child's address field value, you can use the "toolset_association_created" hook:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created
Example:
Suppose that the slug of the location field is "location-address" in both the parent and child post types and the slug of the target relationship is "shop-book".
add_action( 'toolset_association_created', 'update_location_field_association_created', 10, 5 );
function update_location_field_association_created( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
$field_slug = 'location-address';
$target_association = 'shop-book';
if ($target_association == $association_slug ) {
$parent_location = types_render_field( $field_slug, array( 'item' => $parent_id, 'output' => 'raw' ) );
update_post_meta( $child_id, 'wpcf-'.$field_slug, $parent_location );
}
}
Likewise, to make sure that when the parent or child post is updated from the admin area, the parent post's location information is copied to all the connected child posts, you can use "save_post" hook with "toolset_get_related_posts" function:
https://developer.wordpress.org/reference/hooks/save_post/
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
Example:
Suppose that the slug of the location field is "location-address" in both the parent and child post types and the slug of the target relationship is "shop-book".
And the slug of the parent post is "shop" and the slug of the child post is "book".
function update_location_field_on_save( $post_id, $post ){
$parent_post_slug = 'shop';
$child_post_slug = 'book';
$target_association = 'shop-book';
$field_slug = 'location-address';
if ( ($parent_post_slug == $post->post_type) && ($_POST['action'] =='editpost') ) {
$parent_location = types_render_field( $field_slug, array( 'item' => $post_id, 'output' => 'raw' ) );
$get_related_children = toolset_get_related_posts( $post_id, $target_association, 'parent', 999999, 0, array(), 'post_id', 'child' );
foreach ($get_related_children as $get_result ) {
update_post_meta( $get_result, 'wpcf-'.$field_slug, $parent_location );
}
}
else if ( ($child_post_slug == $post->post_type) && ($_POST['action'] =='editpost') )
{
$get_related_parent = toolset_get_related_posts( $post_id, $target_association, 'child', 1, 0, array(), 'post_id', 'parent' );
foreach ($get_related_parent as $get_result ) {
$parent_location = types_render_field( $field_slug, array( 'item' => $get_result, 'output' => 'raw' ) );
update_post_meta( $post_id, 'wpcf-'.$field_slug, $parent_location );
}
}
}
add_action( 'save_post', 'update_location_field_on_save', 30, 2 );
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.
Important 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/