Skip Navigation

[Resolved] Select only related items (from another relationship) in CRED form

This support ticket is created 3 years, 11 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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by sarahK-2 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1924591

I have the following Relationships

ONE Dog to MANY Sessions
MANY Dogs to MANY Groups
MANY Sessions to MANY Groups

On the "Dog" page, that Dog can be connected to any number of Groups
On the "Dog" page, Sessions can be added for that particular Dog

I have a form on the Session page that allows the user to connect that current Session with a Group. Currently, the drop-down contains all Groups. I would like it to only contain Groups that belong to the Dog-Group relationship (of the Dog that owns the Session).

I don't know how to limit the drop-down on the form (connect a Group to this Session) to only those Groups related to the Dog that "owns" the Session.

Thanks in advance for any help you can give me.

#1924907

Hello and thank you for contacting the Toolset support.

The only restriction that can be made in an M2M relationship field is by the author. The field will display only groups created by the same author as the current session. For example:

[cred_field field='@group-session.parent' select_text='--- not set ---' class='form-control' output='bootstrap' author='$current']

The author='$current' attribute can also be used on the cred-relationship-role shortcode.
https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/

Otherwise, this will need to use a generic field, populate the possible values using a shortcode, then saving the relationship after the form submit using a custom hook. The example shared in this reply should explain the idea better and give some inspiration. It's for a different use case, but it shows how to use Toolset relationship functions:
https://toolset.com/forums/topic/parent-cant-be-identified-without-grandparent/#post-1200437

Saving the relationship may require hooking into the cred_save_data and connecting posts using Toolset functions:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

I hope this helps. Let me know if you have any questions.

#1926545

Thanks so much for this answer. I will have to dig into it and see how it goes. I'll let you know if I have additional questions. Cheers!