Skip Navigation

[Resolved] Creating 2 posts with frontend forms and connect them in a many-to-many relation

This support ticket is created 4 years, 9 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 10 replies, has 2 voices.

Last updated by Nigel 4 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#1481585

I am still struggling with how to use the relationships and front end forms correctly, sorry about that:

I have to post types:
- contacts
- actors profiles
related in a many-to-many relationship since many contacts (mothers/sisters) can be connected to on actor profile and a contact can be attached to many actor profiles (like a talent agent).

- Ideally user goes to this page
hidden link

- clicks in add actor on the top right

- fills out first form with actor details:
hidden link

- is automatically redirected to second page with second form:
hidden link

- fills our contact details

- gets automatically redirected to the newly created actor profile:
hidden link

The 2 newly created posts are automatically connected by toolset.

Relationship & Relationship Forms:
hidden link
hidden link

Sorry, for still not getting this and keep coming back. I have read through the documentation on the toolset website and we got it to work with one-to-one relationships, but I am stuck with this one again.

Hope I'll understand it this time 🙂

#1482633

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tobi, with that wish-list of steps, where does it fail?

Which bit doesn't work as intended?

#1482661

Sorry, I've tried to be too specific 🙂

The step that I cannot figure out is:
- The 2 newly created posts are automatically connected by toolset.

#1484253

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Understood.

The user created an actor post, then created a contact post, and wants that contact post to be automatically connected to the originating actor.

Ordinarily this would be a manual step, requiring a relationship form, with adding actors, adding contacts, and connecting contacts with actors three separate activities.

I think the workflow you describe should maybe look slightly different.

These are many-to-many relationships, so the agent post may already exist in the system, the user shouldn't necessarily be adding a new one, they may need to choose from an existing one.

So I would have the form that creates an actor redirect to display the profile for that actor.

That profile includes a form to connect an existing contact, as well as a link to to add a new contact.

The link to add a new contact would add a URL parameter that passes the ID of the actor post the new contact should be linked to.

The form to add the new contact includes a generic field to harvest this ID and make it available to an Forms hook triggered when the form is submitted which uses toolset_connect_posts to join the new contact to the actor, while the form redirects to the actor page.

What about that for a workflow?

I can help with the implementation if you agree.

#1484515

Thanks Nigel. Yeah, I had a hunch that this kind of workflow seems more natural to the way toolset works. I can see how this makes things less complicated, so yeah let’s go for this and I would certainly greatly appreciate your help in setting this up. Looking forward! T

#1487021

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

So your template for actors will include a relationship form to connect existing contacts, which can be inserted directly in the template for actors.

Regarding the part about adding new contacts and having the form redirect back to the actor and to automatically connect them, you

- create a form to publish contacts
- you include a generic field for the actorid (you could use that as the field slug) where the default value is going to come from a URL parameter which, for the purposes of illustration I'm describing as actorid, for which you can use the shortcode

[wpv-search-term param='actorid']

Save the form, and insert it on some page.

Go back to your template for actors to add the link to this form, to which you will append the actor id using the actorid parameter.

You need to manually construct the link, like so:

<a href="[wpv-post-url item='154']?actorid=[wpv-post-id]">Add new contact</a>

Note that 154 is the ID of the page where I have the form to publish contacts inserted. If you use the GUI to insert the wpv-post-url shortcode you can search for the name of the post, or just look it up and add it manually.

So, when viewing the actor you'll see the "Add new contact" link to the page with the form that adds an actorid URL parameter. The form includes a generic field that grabs that actorid parameter value, so that it can then be used in some code triggered by the cred_success_redirect hook, e.g.

function ts_custom_redirect( $url, $post_id, $form_data ){

    // Edit as required
    $relationship = 'actor-contact';
    $param = 'actorid';
    $formid = 152;
    
    if ( $form_data['id'] == $formid && isset( $_POST[$param] ) ){

        $url = get_permalink( $_POST[$param] );

        toolset_connect_posts( $relationship, $_POST[$param], $post_id );
    }

    return $url;
}
add_filter( 'cred_success_redirect', 'ts_custom_redirect', 10, 3 );

This does two things. One is to connect the new contact to the actor. The second is to update the redirect url so that it goes back to the same actor post. (Note, for this to work your form must include a redirect of some kind, which this code will overwrite.)

#1487939

Very detailed and clear instructions, Nigel, thank you so much, works like a charm! My issue is resolved now. Thank you!

#1543259

Hey Nigel, since this is very closely related to the above question, I thought it's maybe okay to reopen this?

Instead of redirecting back to the original page, I would like to redirect to the just created post (basically the 'Display the post' function in CRED form). How do I have to change the above code to achieve that?

Thx a lot! Tobias

#1552411

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tobias

I'm not sure I understand. If you want to redirect to the newly created post ("basically the 'Display the post' function in CRED form") then why don't you remove this code and use the form setting to display the post?

#1552447

Hey Nigel. Sorry I was not clear. I would still like to keep the functionality of automatically connect the posts when sending the form. So keep that functionality of the original code. But instead of redirecting to the page where the form was clicked, I would like to redirect to the post that was just created.

So only connecting posts, not redirecting back to where I came from but going to the newly created post.

Somehow when I remove the code part that redirects and use the form settings to direct to the newly created posts it does not work.

Is that clearer? Cheers and thx Tobias

#1552463

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

In that case move the code to use a different hook, such as cred_save_data, and you don't need to update $url, or return anything (cred_save_data is an action, not a filter).

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data