Skip Navigation

[Resolved] Post Relationship field single select needed

This support ticket is created 4 years, 3 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.

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)

Author
Posts
#1720207
Annotation 2020-07-27 220616.png

Tell us what you are trying to do?

I have a very simple form with two cred fields that are of the parent select variety. These fields need to be single select drop downs, but I'm not seeing a way to make this happen.

Is there any documentation that you are following?

Yes, I've been through all the cred field documentation, I attempted to use the userselect2='never' but the drop downs still seem to be caching earlier selections and adding them to the fields. I have a modal pop-up set up for confirmation that the fields are selected correctly before submitting, and if I switch between entertainers or dances, say I made a mistake and want to change one, all previously selected values show up in the JS modal. I even tried setting single_select to true hoping it would work even though it's not a taxonomy field, but no joy.

Is there a similar example that we can see?

Please see the screenshot attached. The pop-up modal should only allow for a single value to be selected and the small x on the right hand side of the drop down to remove options is unfortunately not a viable solution for my client.

#1721019

Hi, I'm not sure if you made a typo or if you are using the wrong attribute for select2.js...the shortcode should be use_select2="never", but your code says userselect2="never". If you're using the underscore in the actual site but it is still showing a fancy select with an autosuggest typing input, I'll need to take a closer look. Hopefully it is just a typo in the shortcode though, that would be a simple fix!

#1721221

Christian,

Apologies as I was typing from memory and I should have copied and pasted the short code in. Here is the first of the two fields:

[cred_field field='@entertainer-dance.parent' class='form-control' output='bootstrap' use_select2="never" ordering='asc' select_text='--- not set ---']

Now this does work in that it only allows one selection at a time, but if I select an entertainer, then change the entertainer it lists both of them in the modal. I think this is a JS/jQuery issue though, so I'll empty those fields prior to appending the text to them and that should clear this all up.

I did want to ask you though, there are several warnings in the documentation copied below that warn against using use_select2.

"Works only on sites not using the new post relationships (legacy code). If used with new post relationships (Types 3,0+), it breaks the "order", "ordering" and "author" attributes. It can also break background checks like what post type to display in the select field for relationships. Warning: Do not use this attribute if not explicitly required."

I am using the the latest version of Types so the warnings apply, and I'm also using ordering (although it isn't that important). My main concern is if it breaks the post type to display (which I see no signs of now) what should I do then? Is there another work around? Perhaps setting up a generic select field and populating it using a view that spits out JSON objects?

Thanks for your help as always,
Matt

#1721973

Yes, the workaround in that case is to use a generic select field and create its options with a View that outputs a JSON-like string of post IDs and post titles of the parent posts. Then to programmatically set the post relationship based on the selected option, you would use custom code with the Forms API and the Post Relationships API. The Forms API cred_save_data will allow you to access the selected option from the generic field, then the Post Relationships API toolset_connect_posts will allow you to programmatically create the relationship between the child post and the selected parent post. I can provide links to other tickets with examples if you'd like to see how that works. Here are some links to the API documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

#1722137

Christian,

No need I've used that work around several times before and it works well, but I don't think I'll need it here. use_select2="never" appears to be working now (after clearing cache), I just need to edit my jQuery Modal to display only the selected value and not all of them, but that's not a Toolset issue so we are all good now, marking this resolved!

Thanks,
Matt