I have a repeater with two field: post_type (select) and posts (post object). What I need is get the selected post type when the posts field is focused.
What I tried:
acf.add_filter('select2_ajax_data', function(data, args, $input, field){
const post_type = acf.findFields({
name: 'post_type',
sibling: field.$el,
limit: 1,
});
return data;
});
It get the correct field but always the first repeater item.
Found a solution:
JS
acf.add_filter('select2_ajax_data', function(data, args, $input, field){
const post_type_field = acf.findFields({
name: 'post_type',
sibling: field,
limit: 1,
});
const post_type_value = post_type_field.find($('select')).val();
if (post_type_value) {
data.post_type = post_type_value;
}
return data;
});
PHP
add_filter('acf/fields/post_object/query/name=featured_posts', function($args){
if (!empty($_POST['post_type'])) {
$args['post_type'] = esc_attr($_POST['post_type']);
}
return $args;
});