Support

Account

Home Forums Backend Issues (wp-admin) Fill fields after change value in another field Reply To: Fill fields after change value in another field

  • Here is a more complete example .

    UC : you want to change the content of some fields based on the selection of a combo box.

    Here the JS part :

    jQuery(document).ready(($) => {
        acf.addFilter('select2_ajax_data',(data, args, $input, field, instance) => {
                
                // Get the combo field instance
    
                let your_combo = acf.getFields({
                    name: 'your_combo_field_name'
                })[0];
    
                if (your_combo !== undefined && (data.field_key === field.get('key')) {
                        data.your_value = your_combo.val();  // Here is the magic ;)
                    }
                }
            });
    })

    And on PHP side, for all the fields depending on the combo selected value

    foreach (['field1','field2','field3',] as $name) {
    	add_filter( 'acf/fields/post_object/query/name=' . $name, function ( $args, $field, $post_id ) {
    
    		// get the value from AJAX or from the field value
    		$list = $_POST['your_value'] ) ?? get_field( 'your_combo_field_name', $post_id );
    		}
    
    		// change the query according to your model
    		if ( null !== $list ) {
    			// $args['tax_query']
    			// $args['meta_query'] 
    		}
    		return $args;
    	}, 1, 3 );
    }
    

    Hope this helps. Do not hesitate to comment if you find any problem in the code (wrote online from a live example)