Skip Navigation

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

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 6 years, 4 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1098466

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 🙂 ?

#1098809

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Charlotte,

Christian is currently on a public holiday today but he will be back tomorrow to continue helping here.

Thanks,
Shane

#1099537

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

That's it. The rest works.

So long,
Charlotte

#1099713

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?

#1100700

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?

#1101346

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.

Thanks so far,
have a nice day!
Charlotte

#1101355

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"]

If you please help!

greetings
Charlotte

#1101357

Check the Trainingen Template Layout. Where can I find the "meerdere-trainers" View?

<p><span class="kopje">Trainer:[wpv-view name="meerdere-trainers"]</span></p>
#1102386

Hi Christian,

This view (meerdere-trainers) disappeared !???

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?

Have a nice day,
Charlotte

#1103672
Screen Shot 2018-09-09 at 11.20.00 AM.png
Screen Shot 2018-09-09 at 11.20.29 AM.png

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.

#1104307
screenprint 2018-09-10 om 09.06.38.png

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.

Thanks sofar, nearly there,
Charlotte

#1104691

The problem is here in the Loop editor:

[wpv-user field="user_firstname"]
[wpv-user field="user_lastname"]

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

#1107515

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