Home › Forums › Backend Issues (wp-admin) › How can I dynamically filter the choices in a taxonomy select? › Reply To: How can I dynamically filter the choices in a taxonomy select?
Dealing with getting values from and updating values in select2 fields is not really something I’ve tackled. I haven’t quite figured that out yet and I’m always in too much of a hurry when I need it on a project and generally find ways to work around it.
To be honest, I’m not sure that you can alter the values available in the second select2 field. The reason for this is that ACF is doing it’s own AJAX request to get the values that appear there, so in order to substitute your own values you’d probably need to figure out how to make ACF not update the field in the first place, something that I’m not sure is even possible in the first place. Once you do that you’d probably need to look into how to update the select2 choices in the field by calling a select2 method.
I recently had a similar situation where I needed to provide 2 fields for a client that allowed selecting a “Parent” term and a second field to select a “Child” term. This was required to restrict the choices basically so that the client could not screw things up. What I ended up doing was using 2 regular select fields. The first field only shows top level terms in the taxonomy and the second one shows only child terms of the top level term selected. Both fields are required so that they must select a parent and a child term.
Generally, whenever I’m doing complex things like you found in my repo I fall back to using basic fields and coding all the logic myself rather than try to manipulate the ACF select2 fields.
Just a heads up though. The developer is currently looking into adding filters that allow you to manipulate these fields from the JS side https://github.com/AdvancedCustomFields/acf/issues/4
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
❓Ever wondered when and why ACF uses JSON instead of the database? Check out our summary of the most recent session of ACF Chat Friday for the answer, and make sure to register for the next session.— Advanced Custom Fields (@wp_acf) February 23, 2023
© 2023 Advanced Custom Fields.