Home Forums General Issues Dynamic Taxonomy Field based on Dynamic Select Field


Dynamic Taxonomy Field based on Dynamic Select Field

  • Hi – I’m basically creating a page builder with Flexible Content and I’d like to include a ‘Post Grid’ template that allows users to choose which post type they want to display and then give the option to choose a category if they like. What I have at the moment is a select field for the post type that I am dynamically populating with the following:

    add_filter('acf/load_field/name=choose_posts', 'acf_load_post_types');
    function acf_load_post_types( $field ) {
        $choices = get_post_types( array( 
            'public'   => true,), 'objects' );
            unset( $choices['attachment'] );
            unset( $choices['page'] );
            foreach ( $choices as $post_type ) :
                $field['choices'][$post_type->name] = $post_type->labels->singular_name;
            return $field;

    This give me a single select field with the options ‘Post, Testimonials, Services’ (default post type and two CPTs). What I would like to do is have another select field that populates with the categories of the selected post type. So if a user selects ‘Services’ the following select field displays ‘Residential, Commercial, etc’. I want this to be dynamic so that I don’t have to go in and create conditional fields based on the sites categories – especially since clients can create their own categories once the site is handed off. Any help would be appreciated. Thanks

  • It would probably not be impossible to do this, but it would be very difficult and I don’t know how to accomplish it.

    If you have custom taxonomies for each post type would be one thing, but on the other hand if you are using a single taxonomy for all post types then it would get even more complicated.

    In the latter case, that is using a single taxonomy for all post types, it would probably be impossible to do this and allow for terms to be added in the future and to automatically populate the field based on those unknown future terms. The only way that I can think of to work around this problem would be that if the taxonomy is hierarchical and all of the terms for a specific post type are all children of a main term used for that post type.

    If you do have custom taxonomies (or even if you done) then using a taxonomy field is difficult because you would need to intercept the AJAX call that ACF makes to get the list of terms modify the request and inject the selected post type into that request. Then you’d need to build a filter that modifies the get_terms() query that ACF runs

    If I needed to do this I would not use a taxonomy field. I would use another select field and I would populate the values of the second select field dynamically based on the selection of the first field as in this example

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

You must be logged in to reply to this topic.