Home›Toolset Professional Support›[Resolved] Connect post created by Forms to a post in another CPT by the same author, using Post Relationships API
[Resolved] Connect post created by Forms to a post in another CPT by the same author, using Post Relationships API
This thread is resolved. Here is a description of the problem and solution.
Problem: I have two custom post types, Trainers and Trainings, that are connected in a one-to-many relationship. Multiple Trainers can be associated with the same Training post. Each Trainer post is associated with only one User, who is the post author, and each User can only create one Trainer post. Each User can create multiple Training posts. I would like to automatically associate a new Training post created with Forms to the same post author's Trainer post.
Solution:
Use the cred_save_data API to add functionality whenever a new Training post is created. Use the WP get_posts method to determine the post author's Trainer post, and if it exists, use the toolset_connect_posts API to connect the Training and Trainer posts:
add_action('cred_save_data', 'ts_link_author_trainer_to_training',10,2);
function ts_link_author_trainer_to_training($post_id, $form_data)
{
global $current_user;
wp_get_current_user();
// if a specific form
if ($form_data['id']==541)
{
// get the Training post created by the current User
$trainer_args = array(
'post_type'=>'trainer',
'numberposts'=>1,
'author'=>$current_user->ID,
);
$trainer_res = get_posts( $trainer_args );
if(isset($trainer_res[0])) {
$trainer = $trainer_res[0];
// connect the trainer and the training just created by Forms
toolset_connect_posts( 'trainer-activiteit', $trainer->ID, $post_id );
}
}
}
Hi Christian,
I need to get into the problem again :-}
What still needs to be done:
1) When a trainer (=user) makes a new training (=event) (both cpt's), this user is no langer added to the event.
2) As a result: on the eventpage the user is not shown.
Tomorrow I will work on it. In the meantime, maybe the two questions above are easy 🙂 ?
Thanks Shane.
In the meantime I tried to go further, but I do not comprehend how to do it. So for Christian, i will put the problem as straight as I can.
In postform: 'Training aanmaken' (make event) I expect to be able to add the trainer (=user) as a relationship formfield.
There is no such thing so I can't connect the actual user with the training (event).
Hi, neither of the plus account trainer logins I have access to are working now. Can you check the logins and provide an updated trainer login I can use for testing?
Okay I have added the following code to your functions.php file:
add_action('cred_save_data', 'ts_link_author_trainer_to_training',10,2);
function ts_link_author_trainer_to_training($post_id, $form_data)
{
global $current_user;
wp_get_current_user();
// if a specific form
if ($form_data['id']==541)
{
// get the Training post created by the current User
$trainer_args = array(
'post_type'=>'trainer',
'numberposts'=>1,
'author'=>$current_user->ID,
);
$trainer_res = get_posts( $trainer_args );
if(isset($trainer_res[0])) {
$trainer = $trainer_res[0];
// connect the trainer and the training just created by Forms
toolset_connect_posts( 'trainer-activiteit', $trainer->ID, $post_id );
}
}
}
This code should automatically connect the Trainer post and the Training post when a User submits the Training aanmaken Form. Can I test this out on the site, or would you prefer to test it?
Hi Christian,
I tested, made a test-event and indeed via the admin I can see that the Trainer is automatically connected to the post. Only (!) to my surprise it does not show up on the frontend where it should: hidden link
This is the single-page template.
I will leave the test for a while so you can see and test further yourself.
Also the link on the accountpage: 'Connect another Trainer' does not work.
Ai!!! I discovered that in all posts the trainer name has disappeared. I tried to add a new view to the template 'Een training'.
I did not succeed!
So I thought to remove the code from functions.php for now. But still the names (with link to the Trainer-profilepage does not show up. It this piece of code: [wpv-view name="meerdere-trainers"]
I tried to make a new one (connected-trainers), but I don't know how to do it so that the connected trainers are shown. Pfff, I feel confused about the relationships option.
Maybe you will help me with this, otherwise I must have a tutorial. Which is there that I can follow?
We want to show Trainers that are connected to the current Training post. So I added a "Post Relationship" filter and chose to show Trainers in the relationship "Trainer", connected to the post where this View is shown. See the screenshots here. Now you may want to remove the post author shortcode from the Layout so it's not repeated.
Hi Christian,
I don't see the view you made.
I tried to make this same filter in the view: 'Connected trainers' and placed this view in de Layout: 'Een training'.
It does not work. What am I doing wrong?
Please have a look at 'Connected -trainers'
Your activity results in that I see my own name as under Trainer as long as I'm I'm logged in, see screenshot.
The wpv-user shortcode displays information about the current, logged-in User unless it is placed in a View of Users. This is not a View of Users, it's a View of Trainers. So you can use a wpv-post-title shortcode to display the Trainer post title instead. If you need to display the User's information, then you need to determine the author of the Trainer post in the loop using the wpv-post-author shortcode. You can find documentation for these shortcodes here: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user
Hi,
Yes, I mended this 🙂
Only the code to connect another trainer does not work:
[cred-relationship-form-link form='toolset-trainers-trainingen' child_item='$current' layout_slug='toolset-trainers-trainingen-relationship-form']Connect another Trainer[/cred-relationship-form-link]
It leads to a blank page.
I don't have time at the moment, so maybe we should close this topic for now.
Thanks very much for the help,
Have a nice day!
Charlotte