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.

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
#954518
Schermafbeelding 2018-07-24 om 16.02.08.png

I am trying to:
Add a formfield relationship to an existing Cred form

Link to a page where the issue can be seen:
access is restricted

I expected to see:
>Formfields > Relationship

Instead, I got:
Nothing, it is not there, see screenprint

Hope you can help,

Kind regards,
Charlotte

#954609

Hi, if you are using a many-to-many (M2M) relationship, then you must create a Relationship Form to connect two posts. It is not currently possible to manage M2M relationships in a Post Form. If you are using a one-to-many relationship, then the relationship fields will appear here to connect child posts to parent posts. More information about using Relationship Forms for M2M here: https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/

#956512

Hi, tried to do it but failed. I tried to follow the tutorial but my situation is different (?).

I have an existing CRED-postform for making, on the front-end, a training (an event) with all kinds of formfields. The inlogged user = the trainer ... makes an event (with date, location etc.). So one trainer has many events, while an event had one trainer (organiser).
Now I changed the last option, so that one event can have more trainers (organisers).
So I need to give a trainer the option to add another trainer as organiser to the event.
How can I achieve this?
Sunny greetings,
Charlotte

#956638

To let a User select an existing Trainer and connect them to an existing Event, use a Relationship Form. If you want to let a User create a new Trainer and connect them to an existing Event, then two Forms are required - one Create Post Form to create a new Trainer post, and one Relationship Form to link an existing Trainer to an existing Event. The User will first create the new Trainer, then use the Relationship Form to link them up.

#957374

Hi, I do not understand what I can do and I seem to have another problem, I think since upgrading the database this week in order to make the relationships possible.
My user-registration is not working anymore (Cred userform), and all postforms for my users does not work anymore.

I hope you will have a look in my admin to help me.

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/forms-do-not-work-after-migration/

#958297

I have added another ticket to address the issue where your Forms stopped working after migration. Let's focus on the original issue here - you want to create a Relationship Form but can't get it to work. I will be glad to help you with this. May I have access to your wp-admin panel so I can see how things are set up? Private reply fields are active here.

#1000863

So I need to give a trainer the option to add another trainer as organiser to the event.
Will the current Trainer to create a new Trainer post, or select an existing Trainer?
To create a new Trainer, use a Create Post Form.
To associate an existing Trainer with an existing Event, use a Relationship Form.

I will be glad to give more details but first I need to know if you want to allow Trainers to create new Trainers with a Form, or only select existing Trainers.

#1069343
Screen Shot 2018-07-31 at 10.54.47 AM.png

Okay thank you. Here's how I would set this up:
- Create a new Relationship Form for the Trainer relationship - trainer-aktiviteit
- Use the Autogenerate button to generate the Form
- Create a new Layout and insert the Relationship Form using a Relationship Form cell
- When you have inserted the Form cell, you will be shown a dialog like the screenshot here. You will choose to "link a Trainer to a given Training", because the Training is known but the Trainer must be selected. Choose "Use the current Training post".
- Do not assign the Layout to any post type or archives. Leave it unassigned.
- In the Training Template Layout or Content Template, insert a link to this Form by clicking the Toolset Forms button, then click your Form's button in the section "Relationship Links".
- Choose the settings as in the screenshot again.
- Test the link on the Training post. Let me know if you are able to see the Relationship Form on the front-end, and if the correct Training post is selected.

#1070220

- In the Training Template Layout or Content Template, insert a link to this Form by clicking the Toolset Forms button, then click your Form's button in the section "Relationship Links".

Hi, I have come as far as this line above. I don't see that I can put it in a template. Tried but failed.

Where I would like to put it as a link is the 'my-account' page where everything of one trainer is assembled. You may log-in as me (as plus-account trainer).

I have three activities and here I put the relationshipform, but it takes to much space. Can I make a link?
I have put the form in a page: hidden link

I'm nearly there ...
bye

#1070424

Okay I have the relationship links appearing now, but there seems to be a problem displaying the Form. I even recreated the Layout and created a new Form, but still not having luck. I'm asking my 2nd tier support team to take a look and I'll let you know what we find out.

#1071264

Ok, thanks,
I have another plus-account for you:
user: tester
pass: ktP7cW1QE%vj

Till later

#1072594

Hi Christian and others,
Today is my last workday and I'll be back ca. 27 august.
The problem is not solved, but I leave it for a while.

There is another problem that I don't know how to manage, that is: When a trainer makes a new training, the post-author is not connected. I can't figure out how to manage it so that the post-author=connected trainer.
Maybe you can help.
The site is working more or less, I instructed my co-administrator to, when someone makes a new training, connect the trainers via the admin.
I will leave the login for you, but I change the pass of the FTP.

I hope you have a nice summer,
Greetings,
Charlotte

#1074299

Okay I'm still waiting to receive some information from my 2nd tier support team, and I will update this ticket when I have that information. Since you're away, I would prefer not to make changes to your site right now. Let's get together when you return to resume work on this.

#1074300

Yes, good idea!

#1097876

Okay I have received word from my developers that Layouts 2.4.1 included a fix for the issue where the relationship Form is not working correctly in Layouts. Please update to the latest versions of all Toolset plugins, and let's catch up when you are available to resume work on this ticket.