I’m trying to filter a relationship field based on the value of a post object select field.
To test the ajax, I’m using:
//-- JS
$( '#acf-field_571a7d26c9700-input' ).on( 'change', function() {
// getting the post object select value
var po_select = $(this).val();
var data = {
'action': 'my_ajax',
'data': po_select
};
$.post(ajaxurl, data, function(response) {
console.log( po_select );
});
});
//-- Function
function my_ajax() {
// testing the ajax by saving the value in a simple text 'test' field; this works!
$field_key = 'field_571a931c167c7';
$value = $_POST['data'];
update_field( $field_key, $value, 'option' );
wp_die();
}
add_action( 'wp_ajax_my_ajax', __NAMESPACE__ . '\\my_ajax' );
add_action( 'wp_ajax_nopriv_my_ajax', __NAMESPACE__ . '\\my_ajax' );
I think acf/fields/relationship/query will be used to filter the relationship field. However, I’m not sure how to combine this with the ajax. I can successfully filter the field by using:
function my_relationship_query( $args, $field, $post ){
$args['meta_query'] = array(
array(
'key' => 'custom_acf_field',
'value' => '"###"', // the value will be po_select
'compare' => 'LIKE'
)
);
$post = 'field_571a931c167c7';
$post = 'option';
return $args;
}
add_filter('acf/fields/relationship/query', __NAMESPACE__ . '\\my_relationship_query', 10, 3);
Can someone help? Thanks!