I think it is better to duplicate all other charges and connect the duplicated post to the new duplicated rental.
==>
That is not possible due to the current post-relationship structure you setup between Rental and Other charges post type.
The following post already has two related posts - that connects to other_charges posts: hidden link
And the relationship between rental and other-charges is One rental to Many charges.
So, when you duplicate it, and try to connect the other-charges related posts those other-charges posts already have one rental assigned that is the original rental post so you are not allowed to connect the duplicated rental post to those other-charges posts.
I understood your previous reply. That is why I am suggesting to make a duplicate of other charges posts also. In this case we do not connect the duplicate rental with original other charges but we connect the duplicate rental with the duplicate other charges.
ahh ok. so if you want to make a duplicate of other-charges posts as well, you need to add new duplicate posts for the other-charges post type using the standard function wp_insert_post() and you also need to duplicate the associated post meta fields.
So, here is the code I've added to the cred_save_data hook that is added to your functions.php file.
// get the original rental ID
$org_rental_post_id = $form_data['container_id'];
// fetching related posts of original rental post id
$other_charge = toolset_get_related_posts(
$org_rental_post_id,
'rental-other_charge',
'parent',
100,
0,
array(),
'post_id',
'child'
);
global $current_user;
$duplicate_rental_id = $post_id;
// now, you need to write code here that will insert the duplicate other-charges post.
if(!empty($other_charge)){
foreach($other_charge as $k=>$child_post_id):
// get other-charge post
$post = get_post($child_post_id , ARRAY_A );
// configure the duplicate other-charge post for insert
$my_post = array(
'post_title' => $post['post_title'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'other_charge'
);
$insert_id = wp_insert_post( $my_post );
// get fields of original other-charges post
$fields = get_post_meta( $child_post_id );
// update the duplicate other-charge post with these fields
foreach ($fields as $key => $values) {
foreach ($values as $value) {
$value = maybe_unserialize( $value );
add_post_meta($insert_id, $key, $value, false);
}
}
// connecting the duplicate rental with duplicate other-charge post
toolset_connect_posts('rental-other_charge', $duplicate_rental_id, $insert_id );
endforeach;
}
Go to this rental page which is having two related posts for "rental-other_charge" relationship:
=> hidden link
- Click on Renew button and check two duplicate posts inserted with post meta for other-charges posts and it gets connected with the duplicate rental post.
I hope now this is what exactly you wanted to achieve. Happy to help 🙂