Support

Account

Home Forums Backend Issues (wp-admin) taxonomy field filtered by taxonomy on the other taxonomy (yeah I know!)

Solved

taxonomy field filtered by taxonomy on the other taxonomy (yeah I know!)

  • I have a custom post type for Projects, and on that post type I have a section where I’m using ACF to select staff filling 4 roles.

    I initially set it up with Staff as another custom post type with a Roles custom taxonomy, and some other fields for contact info and such. The Project page had 4 relationship fields filtered by the Staff post type and each its own Role term…

    – BUT – I wanted to be able to add Staff on the fly and would prefer to have a checkbox list style input area for these than the two column relationship field interface

    – SO – I realized since I only needed a few fields for each staff member, and ACF can add fields to taxonomies, I could set it up as a custom taxonomy instead of a CPT, and then just use 4 taxonomy fields on the Project Page. This gives me the option to select the interface appearance I wanted and the ability to add Staff on the fly (and then optionally go in to the taxonomy and add contact info later), but then for each role you have to scroll through the whole list of all staff.

    So here’s my actual question: (finally)
    Is there away to limit the taxonomy terms displayed by an ACF field on that taxonomy?

    It’s not the end of the world if users just have to scroll through the whole staff list to select folks for each role, but it would be really nice if I could limit each list to only staff who can actually fill that role.

    Other half-useful thoughts I’ve had

    – setting these up as 4 separate taxonomies and having each role have a totally separate list of Staff terms, but then if somebody could fill multiple roles we’d have to have duplicate records.
    – making the staff taxonomy hierarchical, with the roles as parents and the actual staff as children – makes the list slightly more organized but still can’t be filtered and also has the same problem as above
    -going back to the CPT and relationship fields and finding a way to customize the appearance and add some way to quickly create new Staff posts?

  • See if I understand what you want to do.

    You want to have a taxonomy called “Staff”. To this taxonomy you add each staff member as a term. When editing each staff member in this taxonomy there is another taxonomy field that lets you select terms from the “Roles” taxonomy.

    Next step is to set up multiple taxonomy fields on the “Project” CPT. This taxonomy field lets you select terms from the “Staff” taxonomy that have a specific role selected in the “Role” taxonomy.

  • Yes, that’s it exactly.

  • The answer to the question is, yes, this could be done, because you will have one Staff taxonomy field that matches each Role.

    Using this filter https://www.advancedcustomfields.com/resources/acf-fields-taxonomy-query/ for each field.

    ACF uses get_terms() https://developer.wordpress.org/reference/functions/get_terms/

    Get terms supports meta queries https://developer.wordpress.org/reference/classes/wp_term_query/__construct/

    So, when getting the staff terms you can use the ACF filter to only return staff members that have a specific taxonomy set by using a meta query on the role field.

    However, you will still have a problem adding new staff members on the fly. The reason is that a new staff member will not have the correct roles set. This means that after saving the post the same staff will not be found for that role so it will not be displayed or selected.

    To correct this you will need to create an acf/update_value filter for each field https://www.advancedcustomfields.com/resources/acf-update_value/

    The update value filter will not alter the value. What the update value filter will do is add the new staff member to the correct role. You will need to look at the value, get the selected terms and then look at each term to see if it has the needed role. If it does not then you would use the update_field() function to alter the ACF field associated with the staff term to add the role.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘taxonomy field filtered by taxonomy on the other taxonomy (yeah I know!)’ is closed to new replies.