Support

Account

Home Forums Backend Issues (wp-admin) How can I send an additional field during ajax requests? Reply To: How can I send an additional field during ajax requests?

  • 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.