Skip Navigation

[Resolved] How to limit the number of categories that can be assigned in a post edit form

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

Problem:
When adding/editing posts with a front-end form how to limit the number of categories that can be assigned? A select field allows only one, but checkboxes or multi-select don't have a way of limiting how many terms can be applied.

Solution:
There is no built-in support for this within Toolset.

Probably the best option would be to use a select dropdown and then add the select2 library where the maximum number of terms can be specified.

General directions for this are outlined below: https://toolset.com/forums/topic/category-not-save-values-in-edit-form/#post-1306483

This support ticket is created 5 years, 5 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 8 replies, has 3 voices.

Last updated by Jaime 5 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1301335
5.png
4.png
3.png
2.png
1.png

I have a Edit Form for a CPT and I need to include 3 categories inside (they are the same taxonomy with 3 different fields to select).

The form is working fine, (it saves and show the fields correctly) but when I try to edit again, those 3 taxonomy fields set automatically in the first of them).
-> I've prepared a serie of screenshot in order to explain better the milestone of the problem.
1. Edit form with the 3 fields. I select "Cascos históricos" / "Calles" / "Castillos"
2. Save the Edit form
3. The edit form shows the fields correctly.
4. I go again to the "Edit form"
5. -> PROBLEM // The fields have changed. Now they are "Cascos históricos" / "Cascos históricos" / "Cascos históricos"

I assume it's something related with those 3 same taxonomy field, because if I use only 1 field the form works as expected.
I assume also that a solution would be give the user the option of choose more than 1 category in the dropdowns, but I would prefer not to do that (I prefer 3 fields with the one-value option).

Any suggestions?
🙂

#1301515

Hi Jaime,

Thank you for waiting and I can access the website's admin area now.
( interestingly it works only in Chrome's incognito mode for me )

From the screenshots, it seems you were referring to "Editar contenido - Usuarios Guia produccion".

If that is correct, can you please share the link to a page where this form can be seen? I'd like to understand the exact workflow, to suggest something accordingly.

regards,
Waqar

#1301615

Hi Waqar.
Thanks for your answer.

Actually the screenshots I send to you are not related with "Editar contenido - Usuarios Guia produccion" but with "Edición de localizaciones". But the problem it's the same in both Edit forms.

I updated your user in order you can access to both forms (if you need it).
So, you can go to your user profile here
hidden link

You have your location here
hidden link

You can edit one location here
hidden link

You have your production here
hidden link

You can edit it here
hidden link

I hope this helps you.
If you need anything else from me, just let me know.
🙂

#1302727

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hola Jaime

Waqar is on vacation this week so I'm stepping in to cover.

I tried the site credentials you provided to Waqar but was unable to log in, though it doesn't matter as I was able to reproduce what you are describing on my local test site.

And I would say that what you describe is entirely expected.

The post you are editing with your edit form has several taxonomy terms assigned.

When you include this taxonomy field in your form the options allow for displaying the taxonomy as checkboxes, a multi-select, or a single select.

Naturally when you choose single select it can only display a single term, and if there are multiple it will only show the first one.

If you include a single select field several times, each time it will show the first one.

If you want to display all terms you must choose either checkboxes or multi-select.

You could persist with your current implementation for the original post form, but not for the edit form.

#1305939

Hola Nigel.
(Qué bien que hables español!)

Ok. I understood. So it's working as expected.
It's a pity because I would like to limit the number of selections to 3 in the taxonomy, and with "one select" option it seems better and nicer than "multiselect" for me.

Anyway, is there any way to limit the number of fields selected in a multi-select option? (I think it's the better solution)

Another option could be duplicate 3 times the taxonomie (so I will have 3 different taxonomies with the same fields to have the "one select" option working), but this may give problems with the search form and it doesn't seem like a 'clean' way to solve the problem right?

Gracias!

#1306483

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

If the rationale for what you are doing is to limit the number of options that can be selected then you might want to try something like converting a multiselect dropdown into a select2 dropdown, when you can then specify in the options the maximum number of options that can be selected.

You would need to add the select2 assets to the page, as described here: hidden link

(Although you should be using the WordPress enqueuing system to add the CSS and JS files: https://developer.wordpress.org/themes/basics/including-css-javascript/)

And then instantiate select2 for the dropdown with the setting for the maximum number of options as described here (where you can see an example): hidden link

I think that is the best option given your use case, do you want to try that?

#1306657

Thanks Nigel.
I'll have a look to the links you provided me.
I'm not sure how to do that, but I'll investigate it.

If I need more help I'll write you back.
If you don't mind I'll leave this ticket open meanwhile.

Thanks Nigel.

#1306921

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, let me mark this as awaiting feedback from you and then you can ask away if you have any further questions...

#1307137

My issue is resolved now. Thank you!