Skip Navigation

[Resolved] Get related posts when duplicate post on front end

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 36 replies, has 4 voices.

Last updated by rafaelE-3 4 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1414635

Tell us what you are trying to do?
Hi,

I am following the solution explained in ticket https://toolset.com/forums/topic/retain-checkbox-and-relationship-value-when-duplicate-post-on-front-end/
The post is duplicated but without relationships. How can I use toolset_get_related_posts and toolset_connect_posts to get also the relationships duplicated?

#1415507

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.

#1415751

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.

#1417041

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.

$property = toolset_get_related_post( $form_data['container_id'], 'property-rental', 'parent' );

If a parent Property exists for the original Rental, you will connect the new Rental with that Property:

if( $property ) {
  toolset_connect_posts( 'property-rental', $property, $post_id );
}

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.

#1417669

My issue is resolved now. Thank you!

#1417679

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?

#1420373

You can use the toolset_get_related_posts API to get multiple related posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

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).

#1424227

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;

#1424315

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 );
}

#1426365

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#1427859

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I have set the next reply to private which means only you and I have access to it.

#1431327

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#1432313

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 .

#1432369

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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?

#1432391

I have added it using FTP. The form is added to rental content template. The form name is duplicate-rental