Support

Account

Home Forums ACF PRO 2 select parent the child taxonomy acf

Solved

2 select parent the child taxonomy acf

  • Hi
    I use acf pro
    Iuse this form:
    https://ophthoplus.com/staging/1967/atlas-photo-form/
    Ihave 2 multiselect(same taxonomy)
    Iwant when choosing from type (yhe parent taconomy)
    Then in diagnosis (child taxonomy) display specific from parent Taxonomy.
    How can do it?

  • This is a question that is asked a lot. This is only possible by custom coding. I do not have any examples. This would require adding custom JavaScript to ACF and using the ACF JS API. It would require altering the AJAX values sent when populating the child taxonomy field. It would also require adding an action to the parent taxonomy field that causes ACF to reevaluate the child. As I said, there are no examples of this because no one has figured out how to do it.

    It is possible to use regular select fields, there are examples of doing this for post types, but not taxonomies, but the idea would be the same and you would need to modify them to use a taxonomy. Do a google search for “acf dynamic ajax select example”.

  • Here’s an example of how we solved this problem:

    1. Pass Selected Parent Terms In AJAX
    You need a JS hook to filter the AJAX request of the child field, and add a new argument that contains the chosen parent term(s):

    
    <?php
    add_action('acf/input/admin_footer', 'support_acf_152814_filter_child_ajax');
    function support_acf_152814_filter_child_ajax()
    {
        global $post;
        //areas where this functionality is needed
        if (!$post || !isset($post->ID) || get_post_type($post->ID) != 'my_posy_type')
            return;
    
    ?>
        <script type="text/javascript">
           (function($){
    
    $(document).ready( function() {
        acf.add_filter('select2_ajax_data', function( data, args, $input, field, instance ){
            let parent_field_key = '<parent_field_key>'; // Parent Field
            let target_field_key = '<child_field_key>'; // Child Field
    
            if( data.field_key == target_field_key ){
                let parent = acf.getField(parent_field_key);
                if (parent) {
                    data['parent_terms'] = parent.val(); //This is an array if the parent field is multiselect
                }
            }
    
              return data;
    
        });
    
    });
    
    })(jQuery);
        </script>
    <?php
    
    }
    

    2. Filter Taxonomy Query of the Child Terms
    Here, you need to catch the argument we introduced above, and modify the query accordingly.
    What I do here, is that I get the ids of all children of the selected parents, and I use put them in the ‘include’ option of the terms query:

    
    add_filter('acf/fields/taxonomy/query/key=child_field_key', 'support_acf_152814_child_terms_filter', 10, 3);
    function support_acf_152814_child_terms_filter( $args, $field, $post_id ) {
        // Check if this is an ajax call
        $children_ids = [];
    	if (wp_doing_ajax()) {
    		if (isset($_POST['parent_terms'])) {
    			$parent_terms = $_POST['parent_terms'];
                if (is_array($parent_terms) && !empty($parent_terms)) {
                    foreach ($parent_terms as $parent_id) {
                        $new_children = get_term_children($parent_id, 'disease_categories');
                        if (!is_wp_error( $new_children )) {
                            $children_ids = array_merge($children_ids, $new_children);
                        }
                    }
                }
                $args['include'] = $children_ids;
    		}
    	
    	}
    
        return $args;
    }
    

    I tested the code above and it works in my case. Please @hube2 let me know if this can be improved further.

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

You must be logged in to reply to this topic.