Support

Account

Forum Replies Created

  • @radgh ah that’s a bummer 🙁 Would be good if a future update would include the select2 container id in addition to the field id as part of the args..

  • Thanks for this @radgh . I used the ‘select2_ajax_data’ filter and checked the data.field_key to determine what field triggered the event and when to pass additional params:

    
    if (typeof acf !== 'undefined') {
        acf.add_filter('select2_ajax_data', (data) => {
          // field_587843455430a is the post select field containing the model
          if (data.field_key === 'field_587843455430a') {
                           // field_586ac90dfd68b contains the make value
            data.make_id = document.getElementById('acf-field_586ac90dfd68b').value;
          }
          return data;
        });
    }
    

    Then on the backend I used the ‘acf/fields/post_object/query/key={field_key}’ filter to filter the post query based on make_id:

    
    // filter models based on their make meta_field
    function my_post_object_query( $args, $field, $post_id ) {
        $make_id = $_POST['make_id']; //the new query param from client-side
    
        // add additional constraints to the post query
        $args['meta_key'] = 'make';
        $args['meta_value'] = $make_id;	
    	
        return $args;
    }
    add_filter('acf/fields/post_object/query/key=field_587843455430a', 'my_post_object_query', 10, 3);
    

    I’m using a slightly different approach since I’m using a separate post object field for make but should work similarly with Taxonomy Relationship fields if you use the ‘acf/fields/taxonomy/query’ filter on the backend instead.

Viewing 2 posts - 1 through 2 (of 2 total)