Hello, you will use toolset_get_related_posts or toolset_get_related_post to find any posts related to the original post, in any post relationship. You need the numeric IDs of those posts, and the slugs of those post relationships. Then you will use that information with toolset_connect_posts to link the new duplicate post with the same related posts. The syntax for that code depends on the post relationships and post types involved. The API documentation is available here with examples: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
Click the orange "+More" button to see code examples for each API. If you are working from Nigel's example, you could add custom code near the bottom of the "if" statement where I have added a comment below:
function tssupp_duplicate_post( $post_id, $form_data ) {
if ( $form_data['id'] == 9 ) { // Edit form ID
// get data of original post
$post = get_post( $form_data['container_id'], ARRAY_A );
// update the new post with this data
$post['ID'] = $post_id;
$post['post_title'] = 'Copy of ' . $post['post_title'];
wp_update_post( $post );
// get fields of original post
$fields = get_post_meta( $form_data['container_id'] );
// update the new post with these fields
foreach ($fields as $key => $values) {
foreach ($values as $value) {
$value = maybe_unserialize( $value );
add_post_meta($post_id, $key, $value, false);
}
}
/* ------------ ADD YOUR RELATIONSHIPS CODE HERE -------------- */
}
}
add_action( 'cred_save_data', 'tssupp_duplicate_post', 10, 2 );
I'm glad to help troubleshoot any code you have that doesn't seem to be working correctly. Or, if you want more relevant code examples, I need to know all the information about the post relationships and post types involved here.
The post I want to duplicate is rental post type. Where rental has a parent post called property and other parent post called tenant. I want to get the property and tenant connected to the rental.
Okay here's an example showing how to get the parent Property post from the original post. This assumes you have a one-to-many relationship between Property (one) and Rental (many) post types. It assumes that the post being duplicated is a Rental post. It assumes the relationship slug is property-rental, but that you may need to modify that. You can find the relationship slug in Toolset > Relationships, then edit the Property-Rental relationship.
The code for Tenant is basically the same. Change $property to $tenant and change property-rental to the slug of the Tenant - Rental post relationship.
Thank you, now I can get post parent.
What will be the code to get children of post rental? I have children post called charges. How can I get all charges that are connected to rental post?
Here is an example showing how to get child Charge IDs when you know the parent Rental ID:
$rental_id = 12345; // change this to an actual rental post ID
$charges = toolset_get_related_posts(
$rental_id,
'rental-charges',
[
'query_by_role' => 'parent',
'role_to_return' => 'other'
]
);
Check the relationship slug to be sure it is correct. The default query limit is 100 child posts, but you can modify this option if you expect to have more than 100 charges for any given Rental. The API has many options, including limits and pagination, as well as an option that will return post objects instead of post IDs (IDs are the default).
I get this problem "Fatal error: Uncaught Error: Call to undefined function toolset_get_related_posts()"
I changed the rental id to $rental_id = $post_id;
I recognized that I have added the code out of the function.Now I have added the code inside the function but still the charges are not cloned with the rental.
$rental_id = $post_id; // change this to an actual rental post ID
$other_charges = toolset_get_related_posts(
$rental_id,
'rental-other_charge',
[
'query_by_role' => 'parent',
'role_to_return' => 'other'
]
);
if( $other_charges ) {
toolset_connect_posts( 'rental-other_charge', $other_charges, $post_id );
}
Christian is on vacation. He will get back to work on the upcoming Sunday. I'm stepping in, hope this is ok.
After reviewing the ticket, it looks like you want to connect the posts using Toolset post-relationship API function toolset_connect_posts().
Can you please share problem URL and access details and clarify what posts you would like to connect and I would be happy to assist.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
I checked the "Custom Code" section and I see there is a duplicate code snippet added:
=> hidden link
But the duplicate code snippet it empty - where exactly the code you shared is added and what posts you want to connect at what action? can you please clarify.
I have added the code in functions.php. I am using a form, so when the user click submit the rental post will be duplicated. The posts I want to connect to rental is other_charges .
I checked the functions.php file of your currently active theme:
=> hidden link
I do not see the code you shared is added there - am I looking at the correct place?
On top of that can you please share the following required information so that I can check the issue further.
=> Where on what page you added the form?