Skip Navigation

[Resolved] Create a relationship form to connect a child post list to a parent post

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

Problem:

1) Two post types: "Places list" and "Event", with one-to-many relationship
2) A relationship form for connecting "Places list" and "Event" posts
3) In a single "Event" post, display a post view: query the "Event" posts, in the loop, display the relationship form of above 2)

1 - The loading time of the "Search for a post" select is quite slow.
2 - The list of places are not filtered by "My places" instead I see all the "places" created by everybody.
3 - I see in the list events that are already linked to "My Place"

Solution:

Since you are display multiple relationship forms in the same page, that might conduct the performance problem in some website with large data or running lots of plugins, see the solution in details here:

https://toolset.com/forums/topic/create-a-relationship-form-to-connect-a-child-post-list-to-a-parent-post/#post-1127315

Relevant Documentation:

100% of people find this useful.

This support ticket is created 6 years, 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Author
Posts
#1127186
Screen Shot 2018-10-15 at 00.46.19.png
Screen Shot 2018-10-15 at 00.45.39.png

I have been working on this ticket: https://toolset.com/forums/topic/i-want-to-create-a-form-where-i-can-add-relationship-items-listed-as-a-table/

And thanks to Christian Cox I could understand a little bit how relationships forms work.

I'm trying to do something that I don't know if it is possible.

1 - I have a "Places list", each place can be created by registered users with a "custom role A". This is the parent of the relationship
2 - I create "Events" that only the "custom role B" can create and I want to "custom role A" users to link "Events" to their "Places".
3 - As "custom role A" I want to show each of my "Places" on separate pages with a list of available "Events" to link to that current place.
4 - I want to add to the "loop of events" an "Add to Place" button which will link the "Event" to the "Place"

What I have done by now:

1 - Show the "Place" layout with a list of events that are linked or not to my "Place"
2 - Add a submit button to each event with a relationship form
3 - Show all "Places" to add to an event.
4 - Submit and link the event to the place

Problems

1 - The loading time of the "Search for a post" select is quite slow.
2 - The list of places are not filtered by "My places" instead I see all the "places" created by everybody.
3 - I see in the list events that are already linked to "My Place"

You can see in the attached images what I have done.

Thanks in advance.

#1127315
place.JPG
connect.JPG

Hello,

I assume we are talking about this case:
1) Two post types: "Places list" and "Event", with one-to-many relationship
2) A relationship form for connecting "Places list" and "Event" posts
3) In a single "Event" post, display a post view: query the "Event" posts, in the loop, display the relationship form of above 2)

If it is:

Since you are display multiple relationship forms in the same page, that might conduct the performance problem in some website with large data or running lots of plugins,

when you insert the relationship form, in the dialog window, you should be able to see the instructions:

Connecting a post to a given post or editing existing connections
Create the relationship form.
Create an "editing-mode" template and insert the form into it.
Link to this template from the "display-mode" template or a View.


For example,
1) you can create a content template "editing-mode", in the content, display the relationship form shortcode, like this:

[cred-relationship-form form='my-relationship-form' child_item='$current']

please replace "my-reationship-form" with your relationship form slug.
The attribute child_item='$current' will setup "event" field as current "event" post.

You can generate the shortcode by using option "Connect a Places list to a given Event as a Places list Event", see screenshot connect.JPG

2) In the loop of "Event" posts view(see above #3), display above content template, like this:
[wpv-post-body view_template="editing-mode"]

3) Edit the relationship form "my-relationship-form", find the "Places list" field, add attribute author="$current" to it, for example:
[cred-relationship-role role="parent" author="$current"]
This will make sure to get only "Places list" posts which author is the current logged-in user

See screenshot place.JPG, this should be able to fix the problem #2 you mentioned above

For the problem #3, I assume you are going to display only those "event" posts which don't have a related "Places list" post, you can edit the content template "editing-mode", use [wpv-conditional] shortcode to check if not parent "Places list" post, then display the relationship form shortcode, for example:

[wpv-conditional if="( '[wpv-post-id item="@places-list-event.parent"]' = '' )"]
[cred-relationship-form form='my-relationship-form' child_item='$current']
[/wpv-conditional]
#1127395
Screen Shot 2018-10-15 at 12.29.23.png

Hello Luo Yang,

Thanks for your quick answer. The relationship between "Places" and "Events" is a M2M but I don't want the "Membership A" to choose a "Place that he has not created" (he is not the owner of that place).

I have solved some of the problems.

1 - Choose only between "Places" the "Membership A" has created.

I still have problems with:

1 - In the event list I will show only the "Events" that are not assigned to "Membership A Places".

[wpv-conditional if="( '[wpv-post-id item="@places-list-event.parent"]' = '' )"]
[cred-relationship-form form='my-relationship-form' child_item='$current']
[/wpv-conditional]

I guess this is only for any event not assigned to a any parent and I want to list the ones not assigned to a specific parent.

Question:

Is there any work around to show the Places select as a plain list with maybe a radio or checkbox? The UX of having to type a place is quite weird. I know in custom field types there is a way to display it differently but I can not guess how to do it in case of a relationship form.

At the end what I want is a list of child that I can link to a parent in an easy way.

Thanks again!

#1127981

Q1) I want to list the ones not assigned to a specific parent.
Yes, you are right, in your case, you can consider two post views, for example:
1) Create a post view, query "Events" posts, filter by the relationship relationship between "Places" and "Events", output the related "Events" post IDs, like this:
123, 456, 789
And you will need to use custom codes to remove other extra HTML tags of the view, for example:
https://toolset.com/forums/topic/rawclean-view-output-including-entire-results/#post-377730

2) Create another post view, query "Events" posts, filter by a "Post ID filter":
Exclude posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]
https://toolset.com/documentation/user-guides/filtering-views-query-by-post-id/#include-or-exclude

Pass the step 1) view shortcode to the second view's shortcode:
https://toolset.com/documentation/user-guides/filtering-views-query-by-post-id/#value-set-by-view-shortcode-attribute
Then it should be able to list the ones not assigned to a current "Places" post

Q2) Is there any work around to show the Places select as a plain list with maybe a radio or checkbox?
There isn't such an built-in option within relationship form, and in some website with large data, if there are lots of "Places" posts, the radio or checkbox won't be able work as expected.

#1130570

My issue is resolved now. Thank you! I will come back to you if I have any other issue with this topic. Thanks again!