Skip Navigation

[Resolved] filter options in CRED form child relationship fields

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/Karachi (GMT+05:00)

Author
Posts
#2636679

I have a solution with three CPTs.
-halls
-rooms
-bookings

I have a one to many relationship setup between hall (one) to room (many). This has a slug of "relationship-room-in-this-hall"
I also have a one to many relationship setup between hall (one) to bookings (many). This has a slug of "hall-booking-event".
I also have a one to many relationship setup between rooms (one) to bookings (many). This has a slug of "rooms-booking-event".

In essence this is to support a system whereby a room in a hall can be booked out. A booking will always be for a specific room but I want to be able to show bookings by individual room as well as bookings for all rooms in a hall on different pages. This is the reason for having both the "hall-booking-event" and the "room-booking-event" relationships.

I have created a front end form to add a booking, i.e. using the bookings CPT. In summary, I want a user to be able to select a hall, then select from a list of rooms in that hall and then submit a booking for that room.

On the front-end form, I can add relationship fields for "halls" and "rooms" but these are based on the "hall-booking-event" and "room-booking-event" relationships. The result is that the "rooms" field always queries and returns all rooms regardless of parent "hall".

I am trying to find a way to pre-filter or limit the rooms shown in the rooms relationship to only those belonging to the selected hall, i.e. based on the "relationship-room-in-this-hall" relationship. Can you advise how this can be done?

#2636987

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

As you've created a one-to-many relationship between halls and rooms, I'll assume that while a single hall can have many rooms, a single room can only be part of a single hall.

If that is correct, you don't need a direct relationship between a hall and a booking, because they'll have an indirect connection through the room.

You'll need two relationships:

a). Halls -> Rooms ( one-to-many )
b). Rooms -> Bookings ( one-to-many )

If you know which 'Room' is linked to a 'Booking', you'll also be able to know and filter by the connected 'Hall' ( Halls -> Rooms -> Bookings ).

As for your main question, the feature to select interconnected/filtered Halls and Rooms in the form of adding a new 'Booking' is not available.
( neither in your current setup nor in my suggested one )

A workaround that you can use in this case is to first create a view that shows the 'Rooms' posts, which also has a search field to filter by related 'Halls'.

Inside each 'Room' result loop item, you can include an 'Add new Booking' form for making a booking for that particular room.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2637011

Hi Waqar
Thanks for your reply. I understand the first part of your reply. I will look at simplifying the relationship in the way you suggest.

Unfortunately, in our solution, I think we will still need a single form to add/edit a single booking that allows users to select a "hall" and then only "rooms" belonging to that "hall". The scenario is that we may get a booking request for a particular hall and room but that particular combination is not available and we need to update the request.

I have noticed that the options open to relationship fields on "Create new" forms are different to "Edit existing" forms. On "Create New" forms, it is possible to set the value of the relationship field from a query string parameter.

For "Edit existing" forms, the option to set relationship field values from a query string parameter is not present. With this in mind, is there a "best practice" way to set the "selected" values of relationship fields using javascript.

Thanks and best regards

Nick

#2638157

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Nick,

Thanks for writing back.

> Unfortunately, in our solution, I think we will still need a single form to add/edit a single booking that allows users to select a "hall" and then only "rooms" belonging to that "hall". The scenario is that we may get a booking request for a particular hall and room but that particular combination is not available and we need to update the request.

- Ok, in that case, the built-in relationship fields won't work, because they won't offer the dependent filtering option.

You'll have to include custom select fields in the form and populate and update their values, through custom script and AJAX calls.

For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

> I have noticed that the options open to relationship fields on "Create new" forms are different to "Edit existing" forms. On "Create New" forms, it is possible to set the value of the relationship field from a query string parameter.

For "Edit existing" forms, the option to set relationship field values from a query string parameter is not present. With this in mind, is there a "best practice" way to set the "selected" values of relationship fields using javascript.

- It makes sense that the option to set the default value in the relationship works in the 'add new' form, but not in the 'edit existing' form. When editing an existing post, the field needs to show which related post is already connected.

By default, the relationship field uses the 'select2' script for the autocomplete feature. But it can be disabled using the use_select2='never' attribute in the 'cred_field' shortcode:
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_field

Without 'select2' script, the field would be a regular 'select' type input field.

regards,
Waqar

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