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
#1432393

Minesh
Supporter

Languages: English (English )

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

Yes, I agree that you should have added it using FTP but still, I do not see the code you shared with functions.php file. I can see "pre_get_posts" but not the code you shared. I'm trying to locate the code you shared at the following functions.php file:
=> hidden link

Am I looking at the right place? If yes, whey I do not see the code you added?

#1432399

May be I can give you FTP access so you can check.

#1432401

Minesh
Supporter

Languages: English (English )

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

Sure.

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

#1432409

Minesh
Supporter

Languages: English (English )

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

Thanks for sharing FTP access.

I logged in to FTP and navigate to /wp-content/themes/astra/functions.php file and I do not able see the code you added which you mentioned here:
=> https://toolset.com/forums/topic/get-related-posts-when-duplicate-post-on-front-end/#post-1424315
or
even I do not see the function you shared: tssupp_duplicate_post with functions.php file.

Can you please double-check that code you added at right site and you shared the right to access details or staging site as simply I do not able to locate the code you shared or tell me if I'm looking the code at the wrong place.

#1432411

It seems that I added it by mistake under /wp-includes. But duplicate function is working fine

#1432413

Minesh
Supporter

Languages: English (English )

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

OMG :- thats not the correct place.

Can you please move it to your current theme's functions.php file as any code is not recommended to add to the wp-includes folder. Once you move it to your current theme's astra's functions.php file let me know I will have a look at it.

#1432417

I have moved it to current theme's functions.php.

#1432477

Minesh
Supporter

Languages: English (English )

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

Ok - finally I'm able to see the code 🙂 that's great.

Now, looking at your structure, I can see that you configured the one-to-many post-relationship between Rentals and Other_Charges post types.

I'm on the following single rental page:
=> hidden link

Where I can see the "Renew" button under the section "General details". I see you 've added now the code to funcitons.php file which should be affected when you submit the form with ID 2073.

As I can see the one-to-many post-relationship between Rentals and Other_Charges post types, what post you want to connect?
As you code you shared:

 $rental_id = $post_id; // change this to an actual rental post ID
		 $other_charge = toolset_get_related_posts($rental_id,  
    'rental-other_charge', 
    [
        'query_by_role' => 'parent',  
        'role_to_return' => 'other'
    ]
);
 if( $other_charge ) {
        toolset_connect_posts( 'rental-other_charge', $other_charge, $post_id );
        }

As there is a one-to-many relationship. there might chance that there are more than one related posts available. Do you mean that when you click on the "Renew" button, it should check the available related posts of post-relationship "rental-other_charge" and copy it to the newly created rental post which will be created when you click on "Renew" button?

I mean, lets say I'm on this single rental post:
=> hidden link
- The above post does not have any related posts for "rental-other_charge" relationship. So as there is no related posts avaialble it should not connect any posts but if for instace there are 3 related post available, those 3 post you want to copy to new rental post - correct?

#1432507

Yes exactly that what I want to do.

#1432511

Minesh
Supporter

Languages: English (English )

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

Great - so would you like to share any specific single rental post that I can use to make a test where there are few related posts available for "rental-other_charge" relationship or I can use any rental post? would be better if you can share one so I can make a test and adjust the code if required.

#1432515

You can use any rental post.

#1432521

Minesh
Supporter

Languages: English (English )

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

Can you please check now.

I've adjusted the code inside your form's cred_save_data hook to your theme's functions.php file as given under:

	// getting original rental post id where the form is added
		$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'
                                     );
									 
		 $rental_id = $post_id; // change this to an actual rental post ID		

		// copy the related posts to new rental_id 
	     if(!empty($other_charge )){
			 foreach($other_charge as $k=>$child_post_id):
				toolset_connect_posts( 'rental-other_charge', $rental_id, $child_post_id );
			 endforeach;
		 }

Can you please check now it works as your end as well.

#1432525

Still not working. When I duplicate the rental I can not see the other charges.

#1432565

Minesh
Supporter

Languages: English (English )

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

The code I shared should copy the other-charges from the single rental post from where you are renewing.

It looks like I got it now what's happening.

Consider the following rental post where you can see the two other-charges posts attached in post-relationship "rental-other_charge" :
=> hidden link

Now if you load the above rental post on front-end and click on the "Renew" button:
=> hidden link

Your aim is to fetch the existing related post from the relationship "rental-other_charge" and copy to Renew post but as you remember, you have one-to-many relationship between Rentals and Other_Charges post types so, the original rental post that is the following post from where you are renewing still have the connection with other_charges post that which is one-to-many.
=> hidden link

That is why we are not able to copy the related post to renewal post because if you check the following other-charges post:
=> hidden link
- The above other-charges post is already connected to the original rental post.

Having said that, we need to delete the related posts assigned to original posts and then assigned it to renew post - is that OK with you?

#1432619

I think it is better to duplicate all other charges and connect the duplicated post to the new duplicated rental.