Home › Forums › Backend Issues (wp-admin) › Fill fields after change value in another field › Reply To: Fill fields after change value in another field
In my functions.php:
function fill_data($post_id){
foreach (['sub_title'] as $name) {
add_filter( 'acf/fields/post_object/query/name=' . $name, function ( $args, $field, $post_id ) {
$list = $_POST['your_value'] ?? get_field( 'sub_title', $post_id );
// change the query according to your model
if ( null !== $list ) {
// $args['tax_query']
// $args['meta_query']
}
return $args;
}, 1, 3 );
}
}
function my_scripts_method() {
wp_enqueue_script(
'script',
get_stylesheet_directory_uri() . 'script.js', #your JS file
array( 'jquery' ) #dependencies
);
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
add_action('init', 'fill_data', 10, 1);
In my script.js (in the same folder with functions.php):
jQuery(document).ready(($) => {
acf.addFilter('select2_ajax_data',(data, args, $input, field, instance) => {
// Get the combo field instance
let your_combo = acf.getFields({
name: 'sub_title'
})[0];
if (your_combo !== undefined && (data.field_key === field.get('key')) {
data.your_value = your_combo.val(); // Here is the magic ;)
}
}
});
})
It doesn;t seem to work. I am not sure if I haven’t included correctly the script.js file. Also shouldn’t the php function to be triggered every time I change my choice in my select drop down field? Thank you
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.