Skip Navigation

[Resolved] How to add/delete connected posts in many to many relationship on frontend

This thread is resolved. Here is a description of the problem and solution.

Problem:

I have a many-to-many Student–Donor relationship. On the Donor template, I can list linked Students, but I need to add an existing Student to the Donor and also remove a linked Student on the front end. The disconnect option didn’t appear in my Toolset Forms, so I wasn’t sure how to add a Remove link.

Solution:

Created a Relationship Form to connect existing Students to the current Donor. For disconnecting, used the shortcode [cred-delete-relationship] inside the View loop of Students. The issue was finally resolved after removing a duplicate many-to-many relationship, and the following shortcode worked:
[cred-delete-relationship role_items='$fromViews' relationship='student-donor' redirect='self']Remove[/cred-delete-relationship]

Relevant Documentation:

https://toolset.com/course-lesson/front-end-relationship-forms-for-connecting-posts/

https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred-delete-relationship

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.

This topic contains 3 replies, has 1 voice.

Last updated by raffyC 2 days, 1 hour ago.

Assisted by: Christopher Amirian.

Author
Posts
#2827443

I have a student-donor many to many relationship. In donor template, I added a view to display all the linked students. Now, I need a way to:
1. add an existing student linked to the current donor in frontend
2. delete an existing student linked to the current donor in frontend

I tried the solution to this support ticket but I got issues with the short code and I don't have an idea what it's supposed to look like in the front end.
https://toolset.com/forums/topic/front-end-editing-of-post-relationships/

I don't have a similar example.

Here's the link to the donor template: hidden link

Thank you.

#2827483

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. Please follow the steps mentioned in the documentation below:

https://toolset.com/course-lesson/front-end-relationship-forms-for-connecting-posts/

1) Create a form to connect an existing Student to the current Donor

- Go to Toolset → Forms → Add New and choose Relationship Form.
- Pick your many-to-many relationship (e.g. student-donor).
- In the Form will setting, choose Create new relationship (connect items).
- In Which items will this form connect, set:
.The side that represents Donor to Current post (so the Donor is pre-selected from the template you place this form on).
.The side that represents Student to Let the user choose an existing item (autocomplete list).
- Save the form.
- On the Donor template (where a single Donor is displayed), insert the form and lock the Donor by passing the current post ID.
- Use the shortcode attributes that match your relationship’s sides (parent/child names depend on how the relationship was defined).
- For example, if Donor is the parent side and Student is the child side:

[cred-relationship-form form='connect-student-to-donor' parent_item='[wpv-post-id]']

- If your sides are reversed (Student is “parent”, Donor is “child”), swap the attributes accordingly:

[cred-relationship-form form='connect-student-to-donor' child_item='[wpv-post-id]']

2) Create a form to disconnect a Student from the current Donor

- Add another Relationship Form for the same relationship.
- In Form will, choose Disconnect related items.
- Save.

You’ll typically render this inside the View that lists related Students on a Donor page, so each row has its own “Remove” button.

Inside the View loop of Students (where the loop item is the Student and the current page is the Donor), insert the form and pass both item IDs. Example when Donor is the parent side and Student is the child side:

[cred-relationship-form 
  form='disconnect-student-from-donor' 
  parent_item='[wpv-post-id item="$current_page"]' 
  child_item='[wpv-post-id]']

Thanks.

#2827623
relationship-form-settings.jpg

Hi,

Regarding
2) Create a form to disconnect a Student from the current Donor
- In Form will, choose Disconnect related items.

When I create a new Relationship Form, there's no option for Disconnecting related items. I'm using Toolset Forms 2.6.23.
Please clarify how add a Remove link inside the View loop of Students.

Thank you.

#2827940

Christopher Amirian
Supporter

Languages: English (English )

#2828146

My issue is now resolved after removing a duplicate many-to-many relationship. This shortcode now works.
[cred-delete-relationship role_items='$fromViews' relationship='student-donor' redirect='self']Remove[/cred-delete-relationship]

Thank you!