Support

Account

Home Forums Backend Issues (wp-admin) How can I send an additional field during ajax requests? Reply To: How can I send an additional field during ajax requests?

  • (EDIT: This solution stopped working for me, but I found a new one with Elliot’s help. See my new comment.)

    PROBLEM SOLVED!!

    I found a solution.

    It’s not very pretty, but it certainly works. None of the filters I found in the source code offered a way to extend the arguments sent in a select2 ajax request.

    However, there is a filter to modify the select2 arguments. The annoying part is that the select2 arguments are actually a callback to an ACF function. So the magic here is that we replace that function with a “wrapper” function. This new function still calls the old function, then it adds the “Make” to the results. This data is then sent via ajax, and our filter can now show the correctly models!

    Here is the JavaScript, hard-coded by field ID for the MAKE:

    // Update the ajax arguments for select2 objects to include the Make value
    // This is done by replacing the default ajax.data function with a wrapper, which calls the old function and then appends "vehicle_make" to the results.
    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.vehicle_make = function() {
                        // Add the vehicle make to the ajax function. This happens for all select 2 objects, even if it's blank.
                        return jQuery('#acf-field_55890984e822f').val();
                    };
    
                    // Return the default args with our vehicle_make function.
                    return default_response;
                }
            }
    
            return args;
        }
    );

    Here is the PHP Filter which limits the MODEL by term parent:

    function oss_filter_model_dropdown_admin( $args, $field, $post_id ) {
        // Look for the vehicle make in the AJAX request.
        $make = isset($_REQUEST['vehicle_make']) ? (int) $_REQUEST['vehicle_make'] : false;
    
        // If not found, use the previous vehicle's value.
        if ( $make === false ) $make = get_field( 'make_term', $post_id );
    
        // If no make has been selected, do not show any terms (-1 will never give results)
        // Otherwise, use the make as the parent term. Models are always a child of a make.
        if ( !$make ) $args['parent'] = -1;
        else $args['parent'] = (int) $make;
    
        return $args;
    }
    add_filter( 'acf/fields/taxonomy/query/name=model_term', 'oss_filter_model_dropdown_admin', 20, 3 );

    Here is a video demonstrating the end result, showing that the “Model” field’s options are directly tied to the “Make”.

    http://gfycat.com/RevolvingSleepyBarasingha