Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
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.
Because as I explained with my previous reply:
=> https://toolset.com/forums/topic/get-related-posts-when-duplicate-post-on-front-end/page/3/#post-1432565
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.
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
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;
}
More info:
=> https://developer.wordpress.org/reference/functions/wp_insert_post/
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 🙂
Yes, this is exactly what I wanted to achieve. Many thanks for your support.
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Great - glad to help and happy to know that you achieved exactly what you wanted using the solution I shared. We can close it here I guess.
Happy New year in advance 🙂
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hi Nabils,
Please feel free to mark resolve this ticket 🙂 Happy new year.
Thanks Christian! You are the best! 🙂