Support

Account

Home Forums Backend Issues (wp-admin) ACF taxonomy dropdown using ajax

Helping

ACF taxonomy dropdown using ajax

  • Hi, i have two taxonomy dropdown using the same taxonomy, for the first i’m doing something like this :

    function firstdropdownfilter( $args, $field, $post_id ) {
        $args['parent'] = 0;
    
        return $args;
    }
    add_filter( 'acf/fields/taxonomy/query/name=firstdropdwon', 
    'firstdropdownfilter', 20, 3 );

    The second dropdown, it must be “influenced” form what i’ve selected in the first .. and i’m doing like this :
    JS

    acf.add_filter(
    'select2_args',
    function( args ) {
    
        if ( typeof args.ajax.data == 'function' ) {
            var old_data_func = args.ajax.data; // We'll keep this for maximum compatibility, and extend it.
    
            args.ajax.data = function(term, page) {
                var default_response = old_data_func( term, page ); // Call the old, default function.
    
                default_response.firstdrpdwn = function() {
                    return jQuery('#acf-field_55890984e822f').val();
                };
    
                return default_response;
            }
        }
    
        return args;
    }
    );

    PHP :

    function seconddropdownfilter( $args, $field, $post_id ) {
        $make = isset($_REQUEST['firstdrpwn']) ? (int) $_REQUEST['firstdrpwn'] : 
        false;
    
        if ( !$make ) $args['parent'] = -1;
        else $args['parent'] = (int) $make;
    
        return $args;
    }
    add_filter( 'acf/fields/taxonomy/query/name=seconddropdown', 
    'seconddropdownfilter', 20, 3 );

    The first it’s OK, the second does not working .. I don’t know if i’m using select2 .. Please help me
    Thanks

  • Hi, i’m trying another way.. but obviously it doesn’t work..
    thank’s a lot to anyone can help me :
    A “second” dropwdown called “456” must have the value selected on the first called “123” ..
    PHP

    function my_acf_input_admin_footer() {
    	
    ?>
    <script type="text/javascript">
    (function($) {
    	
    	var x = document.getElementById("acf-field_123"); //first dropdown field
    
    	if (x.addEventListener) {                    // For all major browsers, except IE 8 and earlier
        	x.addEventListener("change", myFunction);
    	} else if (x.attachEvent) {                  // For IE 8 and earlier versions
        	x.attachEvent("onchange", myFunction);
    	}	
    	function myFunction(){
    		var x = document.getElementById("acf-field_123");
    		var y = document.getElementById("acf-field_456"); //second dropdown
    		x.value=y.value;
    	}
    	
    })(jQuery);	
    </script>
    <?php
    		
    }
    
    add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘ACF taxonomy dropdown using ajax’ is closed to new replies.