Home › Toolset Professional Support › [Resolved] Trying to get a relationship to connect two content types automatically
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 16 replies, has 2 voices.
Last updated by Minesh 1 year, 3 months ago.
Assisted by: Minesh.
I have set up a new relationship between Ride Leader and Registration content types. The Registration content type is created through a Post Form on a Ride page. There is also a relationship between Registration and Ride. When the Registration form is submitted, the relationship is getting established correctly with the Ride content type.
The Ride content type also has a relationship with the Ride Leader content type. That connection is made manually at the point we create new Rides. So the Ride Leader connection is always there with each Ride *before* any Registrations are submitted. I thought that the submission of the Registration would automatically connect with the Ride Leader connected to each Ride, as well as connect to the Ride, but that hasn't happened. It seems that I have to manually connect each Registration with each Ride Leader. Is that because the form is on the Ride page and not on the Ride Leader page?
Is there any way for that connection to happen automatically instead to connect the Registration to both the Ride post it is on AND pull the Ride Leader connection from Rides to also connect to that Ride Leader? If we have to manually connect the Registration to the Ride Leader every time, that will kill everything I'm trying to set up with this because that will require too much ongoing manual work for what it is worth as our Registration system becomes more popular to use with our members.
On this, I'm seeing this custom code that was previously supplied to us within a code snippet that was used to create the title for a Registration in a specific way. I'm thinking this code is how the connection is being automatically established on the relationship between Ride and Registration.
/// connecting the posts toolset_connect_posts('ride-registration',$parent_ride_id,$post_id);
Am I correct that this is the code that is accomplishing that automatic connection?
If so, then I'm thinking I just need another line such as
toolset_connect_posts('ride-registration' and then whatever else is needed to pull the Ride Leader connection to the $parent_ride_id,$post_id)
Hello. Thank you for contacting the Toolset support.
Yes, you can connect the two posts (parent and child) using the post relationship function: toolset_connect_posts
More info:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
As you can see with the above doc the following arguments you will require to pass to the function:
string|string[] $relationship
- The slug of the relationship or an array with the parent and the child post type of a legacy relationship.
int|WP_Post $parent
- The parent post to connect.
int|WP_Post $child
- The child post to connect.
int|WP_Post|null $intermediary Optional.
- The intermediary post to use for a many-to-many post relationship. If it is not provided and it is needed, a new post will be created.
Do you want to connect the child post to multiple parent posts? to what post relationship you want to use for both posts?
Good to know I was on the right track with the toolset_connect_posts code found.
So now it's just a matter of the syntax needed.
Ride is the parent. Ride Leader is a child of Ride. Registration is a child of Ride. But then Registration is also a child of Ride Leader now as well.
The post form that creates Registrations is on the Ride pages. I'm not clear on how to connect Registration to Ride Leader given these particulars.
Can you please share admin access details and let me review your current setup.
*** 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.
Can you please share URL of the page from where the new registrations are made for the ride?
Is the Ride Leader is available to select with the registration form?
The registration form is on every Ride page. Here's one as an example:
hidden link
The Ride Leader is not a field within the registration form. There is a Ride Leader that will be connected to every Ride through that relationship. The Ride to Ride Leader connection is made manually by the admin when setting up every new Ride that gets added to the system. Every Ride the Registration form is on will have that Ride Leader connection already made prior to any Registrations getting added.
Then the Registration form is on each Ride page and you got me the code I included above that establishes the Ride to Ride Registration connection through that relationship automatically every time a Registration is submitted.
Now what is needed is a second line of code to also automatically connect the Registration to the Ride Leader that is already connected to that Ride page.
I seriously hope this is possible in some way. Otherwise a lot of functionality already built will be for nothing because if we have to manually connect each Registration to each Ride Leader, that is going to be too much manual work for the admin for what it is worth with the rest of the functionality already built for connecting Registrations to Ride Leaders. Getting this connection automated is the key piece to the puzzle. I should have started with this before building the rest of the functionality, but I didn't realize this connection had to be custom coded to happen automatically. I errantly thought the connection did happen automatically. I didn't notice that connection line of code in the code snippet you guys provided previously until after I realized the connection wasn't happening automatically. I looked through the code to see if I could figure out how to make it happen, and saw this connect code section, but I don't know the syntax that would be needed to also pull the Ride Leader for a second connection.
The Ride Leader is not a field within the registration form. There is a Ride Leader that will be connected to every Ride through that relationship. The Ride to Ride Leader connection is made manually by the admin when setting up every new Ride that gets added to the system. Every Ride the Registration form is on will have that Ride Leader connection already made prior to any Registrations getting added.
Then the Registration form is on each Ride page and you got me the code I included above that establishes the Ride to Ride Registration connection through that relationship automatically every time a Registration is submitted.
Now what is needed is a second line of code to also automatically connect the Registration to the Ride Leader that is already connected to that Ride page.
===>
Lets assume that I created a new registration to Ride but Where I confuse is how to know what Ride Leader we need to connect to this newly created registration? How do I know what Ride leader I have to connect while making new registration?
With the Registration form being on the Ride page, I understand that you're making the connection to Ride through it being the postid, right?
I thought there would be a way to check who is the Ride Leader that has been connected to that Ride postid and then connect the Registration to that Ride Leader as well. Is there no way to check which Ride Leader is connected to the Ride? If you can do that check, then shouldn't there a way to get the postid for that Ride Leader and connect the Registration to Ride Leader that way?
What about adding a hidden field to the Registration form and push the Ride Leader that is assigned to that Ride into that field so it's on the form? Would there be a way to do that? I know we were previously able to push the e-mail address field for the Ride Leader for each Ride into being a recipient for the Notification form. So I'm thinking there should be a way to pull something usable with the Ride Leader into the mix here in a way that would allow the connection code to work.
So taking the following ride post as example:
- hidden link
If I can do the new registration using the form available to register to above ride under label "Register for this Ride", If I do registration then the above ride is connected to the "Kenny Moore" is the ride leader.
=> hidden link
Now, you want the registration post should be connected to the post relationship "ride-leader-registration" where the "Kenny Moore" will be the parent and the newly registered post registration will be child. Is that correct?
I want Registration to be the child of BOTH Ride and Ride Leader, if possible.
I don't know if this would be helpful or not, but the Ride Contact field on all Ride pages is also the Ride Leader. The options for the Ride Contact select field are pulled from Ride Leader post titles.
We're trying to get two pieces of functionality here.
1) Keep the existing functionality that allows Registrations for each individual Ride. I have the view set up that will show all Registrations for a single Ride on that Ride page. Please don't do anything that will modify that functionality as it is working as it should. That uses the Rides to Ride Registrations relationship.
2) Now we're adding functionality that shows ALL Registrations for ALL Rides led by a particular Ride Leader. That uses the newly created Ride Leaders to Ride Registrations relationship. We have all functionality done with that EXCEPT for this automatic connection between each Registration with the Ride Leader.
We want to have both as ways for Ride Leaders to notify riders. We have the Notifications system working for both approaches already as well. The reason for adding the ability to Ride Leaders to notify *everyone* that has *ever* Registered for any of their rides is a stated concern by one of our Ride Leaders that said he doesn't think his regular riders are going to register every time for any Rides that are held on a weekly basis. So we decided to add this My Riders functionality to give each Ride Leader the ability to notify EVERYONE that's registered once for any of his/her rides. But that requires all Registrations to automatically connect to the Ride Leaders as well. Otherwise our admin will have to manually connect every Registration to every Ride Leader and that will be more work than it is worth.
With the custom code snippet namely "registration-title" added to "Custom Code" section offered by Toolset:
=> hidden link
I've adjusted the code as given under:
add_action('cred_save_data', function($post_id, $form_data) { if ($form_data['id']==2083) { // speccific post form ID $name = get_post_meta($post_id, 'wpcf-name', true); $email = get_post_meta($post_id, 'wpcf-email', true); $date = get_the_date('m/d/Y', $post_id); /// getting parent ride id $parent_ride_id = $_POST['_cred_cred_prefix_cred_container_id']; $parent_ride_title = get_the_title($parent_ride_id); /// connecting the ride parent posts toolset_connect_posts('ride-registration',$parent_ride_id,$post_id); /// connecting the ride leader parent posts $parent_ride_leader_id = get_post_meta($parent_ride_id,'wpcf-ride-contact-2',true); toolset_connect_posts('ride-leader-registration',$parent_ride_leader_id ,$post_id); $post_title = "[".$parent_ride_title. "] Registration - [".$name."] [".$date."]"; /// $post_title = sprintf('[%1$s] Registration - [%2$s] [%3$s]', $parent_ride_title, $name, $date); $slug = sanitize_title($post_title); wp_update_post(array( 'ID'=>$post_id, 'post_title'=>$post_title, 'post_name' => $slug )); } },10, 2);
Where:
I've added the following lines of the code:
/// connecting the ride leader parent posts $parent_ride_leader_id = get_post_meta($parent_ride_id,'wpcf-ride-contact-2',true); toolset_connect_posts('ride-leader-registration',$parent_ride_leader_id ,$post_id);
Can you please confirm its working as expected now.
Your test registration did connect the Ride Leader correctly. I've delete your test registration so you won't receive any notifications about this ride. But prior to deleting it, you were on my Ride Leader page here:
hidden link
So it looks like it worked.
We're going to add our August rides to the system shortly. Let me just make sure all is working when Registrations for those Rides start coming in before I mark this one as resolved. I think it's good though.
You're welcome to mark this ticket resolved when you can. Glad to help.