Support

Account

Home Forums Backend Issues (wp-admin) Select sibling field inside select2_ajax_data filter

Solved

Select sibling field inside select2_ajax_data filter

  • 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;
    });
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.