Skip Navigation

[Resolved] Editing Forms for old posts with many-to-many relationships

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

Problem: I would like to understand how to manage M2M relationships with Forms and Views.

Solution: Use Views and Relationship Forms to manage relationships between M2M posts. See the sandbox site details in the ticket for more information.

This support ticket is created 3 years 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)

This topic contains 13 replies, has 2 voices.

Last updated by igorL-3 3 years ago.

Assisted by: Christian Cox.

Author
Posts
#2019379

Tell us what you are trying to do? I have created a Form to edit old posts. It all works very well. When I click on the edit link the form is brought up populated with the old data. But how do I populate the many-to-may relationships with the data that's alredy linked? To explain it better. The old post has a link to another post in a manny-to-many relationship. If I want to edit one of these old posts how do I create a form so that it's alredy populated with the stored many-to-many relationship data as is the case with ordinary forms to edit old posts. As this is shown in the link to a help document bellow.

Is there any documentation that you are following? https://toolset.com/course-lesson/front-end-forms-for-editing-content/?utm_source=plugin&utm_campaign=forms&utm_medium=gui&utm_term=forms-editing-doc

Is there a similar example that we can see?

What is the link to your site?

#2019575

Hello, in the typical workflow an edit post Form does not allow you to manage many-to-many (M2M) relationships, it only edits the main content, taxonomies, and custom fields of the post itself. Normally M2M relationships are managed in a separate Relationship Form, which does not allow editing the main post contents, taxonomies or custom fields. Those processes are usually kept separate. In a one-to-many relationship, an edit post form for the child post type will allow you to choose a parent post, but in a M2M relationship the situation is more complex and requires a separate Form as explained in our lessons here:
https://toolset.com/course-lesson/front-end-relationship-forms-for-connecting-posts/

So the preferred solution here is a separate Relationship Form to manage M2M relationships. If you want to implement a more custom solution that allows you to manage M2M relationships directly in an edit post Form, we have several PHP APIs available for inspecting and managing post relationships with Forms submissions:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Please let me know if you have questions about that.

#2019875

That’s ok if the many-to-many forms are separate from the post edit form. I don’t mind that. What I’m interested in is whether, just like with the ordinary post form, it is possible to populate the fields of the MTM form with the value of the posts that have already been linked. For Eg. Say I have POSTS type CHILDREN and TOYS that are in MTM relationship. Say I already have a post FREDDY that’s linked to TEDDY BEAR. So I go onto the post FREEDY and click the Edit link that takes me to a POST FORM so I submit it again and that takes me to where? Typically that sort of operation takes one to the post itself where one can make new associations like in the Speakers example. But since FREDDY is already linked to TEDDY BEAR can that field (TOY) be populated automatically with TEDDY BEAR or do I need to do some customisation?

#2019925

Say I already have a post FREDDY that’s linked to TEDDY BEAR. So I go onto the post FREEDY and click the Edit link that takes me to a POST FORM so I submit it again and that takes me to where?
Well if you place an edit post link in the Freddy post template, then the edit link will take you to an Edit Post Form to edit the Freddy post. But again, an edit post form does not allow you to manage relationships. Instead of an Edit Post link, you can implement Relationship Form links.

There are several ways to implement relationship forms, depending on what you want to achieve. Each relationship form typically allows you to connect one Child post and one Toy post. In the typical workflow on the Freddy post, you will display a View of all related Toy posts. Teddy Bear is related to Freddy, so it will appear in the list of related posts. In the View of related Toy posts, you can display a link next to each related Toy post that allows you to delete that post relationship between Freddy and that Toy post, so there is no longer an M2M relationship between the two posts. Or you can display a link to a relationship form where the Child Freddy is preselected, and the User can choose another Toy post, like Puzzle, from a list of Toy posts, and create a new relationship between Freddy and that Toy post.

If there are custom fields associated with the relationship, then you can display a link next to each Toy in the View that allows the User to edit the fields connected to that relationship instance.

So it depends on what you want to achieve in the relationship form. If you'd like to elaborate, I can give you more information about setting up any of these actions in the Child post template.

#2020039

>...you will display a View of all related Toy posts. Teddy Bear is related to Freddy, so it will appear in the list of related posts. In the >View of related Toy posts, you can display a link next to each related Toy post that allows you to delete that post relationship >between Freddy and that Toy post, so there is no longer an M2M relationship between the two posts. Or you can display a link to a >relationship form where the Child Freddy is preselected, and the User can choose another Toy post, like Puzzle, from a list of Toy >posts, and create a new relationship between Freddy and that Toy post.

I like to be able to do both. Delete a a relationship or add another relationship without deleting the privious. So that FREDDY can have either only TEDDY BEAR or PUZZLE or TEDDY BEAR and PUZZLE or even nothing by removing all the relationships.

>If there are custom fields associated with the relationship, then you can display a link next to each Toy in the View that allows the >User to edit the fields connected to that relationship instance.

There are no custom fields to be displayed.

Thank you in advance

#2020781

Okay I can help you set up the relationship forms and actions in your site if you need assistance. Do you want to provide a login so I can give you some additional instructions in wp-admin?

#2020839

I'd love to but this is a local dev site not acessible from outside. Cold you give me a link to documentation or examples how to set this up. If I could see the example with the CHILDREN and TOYS that would help a great deal

#2020859

Sure, here is the relationship form documentation you can use as a reference:
https://toolset.com/course-lesson/front-end-relationship-forms-for-connecting-posts/

To disconnect a relationship in a View of related posts, you'll need to use a shortcode since Blocks does not currently provide this feature:
https://toolset.com/forums/topic/disconnect-custom-post-type-relationships-from-frontend-or-via-frontend-form/

I will send you information about collaborating in our sandbox environment momentarily in a private reply.

#2021005

Hi Christian,

I have done as you have asked and created a site in the sandbox. I made two times of posts Children and Toys, in a many-to-many relationship, and connected some toys and children.

#2021121

Just done it. Sorry didn't see it earlier

#2021259
relationship-link.png
relationship-link-configurations.png
fields-and-text-form-icon.png
relationship-template-given-child.png

Okay thank you. I have created a Content Template for Child posts here:
hidden link
This template is used to manage the design of all Child posts. Inside that template, I have created a View of related Toy posts. Now on each Child post, you can see a View of related Toys. Inside that View of Toys, I inserted the following shortcode using a Custom HTML block:

[cred-delete-relationship role_items='$fromViews' relationship='child-toy' redirect='self']Disconnect this Toy[/cred-delete-relationship]

Now in the View of related Toy posts, you can see a link to disconnect that Toy from the current Child.

I created a new Relationship Form to manage the relationship between Child and Toy posts here:
hidden link

I inserted this Relationship Form in a new Content Template here, and set it to connect a given Child and any Toy (screenshot relationship-template-given-child.png):
https://liuba-28215-dog-walkers-1.discover-wp.com/wp-admin/post.php?post=994&action=edit

Then I edited the Child post Content Template again here:
hidden link
Below the View, I inserted a Fields and Text block and clicked the Forms icon (screenshot fields-and-text-form-icon.png) to insert a Relationship Form Link to connect the given Child to any Toy (screenshots relationship-link.png and relationship-link-configurations.png). That produced the following shortcode in the Fields and Text block:

[cred-relationship-form-link form='connect-child-to-toy' parent_item='$current' content_template_slug='994']Connect another Toy[/cred-relationship-form-link]

I saved that change to the Content Template. Now when I visit any Child post on the front-end of the site, I see links to each related Toy post, a link to disconnect each related Toy post, and another link to connect any Toy post to the current Child. Example:
https://liuba-28215-dog-walkers-1.discover-wp.com/child/freddy/

That handles all the M2M management for this relationship from the Child post side. You could do something similar from the Toy post side if you want, but instead of a given Child you would connect a given Toy. Hopefully this helps you understand how to use delete relationship links, relationship forms, and relationship form links effectively to manage M2M relationships between existing posts. Let me know if you have questions about this.

#2021949

Tank you very much. THis is very helpful.

I'm not clear about VIEWS. How and where do you create them and what is the syntax?

#2022021

I'm not clear about VIEWS. How and where do you create them and what is the syntax?
The standard way to create Views is by placing a View block in the Block Editor, inside a Content Template or inside the post contents or page contents where you want to display the View.
https://toolset.com/course-lesson/creating-a-view/

That is how I created the View of related posts - I inserted a View block inside the Content Template to create a new View, and configured it to display related Toy posts.

There is also a legacy Views editor experience, which is available in Toolset > Settings > General tab. That method of creating Views is considered a legacy feature. It is still supported, but it is not usually necessary anymore since the Block Editor version has become available. The legacy Views editor will create most of the syntax for you automatically, as it is a shortcode-based approach. There are a few special cases where the legacy editor is required for a specific feature, and if you need to implement one of these special cases our support team can assist you.

Views Shortcodes are documented here: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/

#2024849

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.