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)
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.