ACF Releationship meta_query query
I have a relationship field displayed on certain posts and I want to populate it with articles only from a certain author ID.
I also want to run a meta_query to check an array of IDs, and if this array finds an ID matching the author ID, to include the articles associated with it:
function reference_sort( $args, $field, $post_id ) {
$author_id = get_post_field ('post_author', $post_id);
$args['author']= $author_id;
$args['post_type'] = 'reference_cases';
$args['orderby'] = 'date';
$args['order'] = 'DESC';
$args['posts_per_page'] = 40;
$args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'field_5f1ba9c9a725b',
'value' => $author_id,
'compare' => 'EXISTS',
)
);
// return
return $args;
}
add_filter('acf/fields/relationship/query/key=field_5f46f9c29272c', 'reference_sort', 10, 3);
Everything works apart from the meta_query which breaks everything.
I am using the EXISTS compare value because I assume it checks to see if the ID is in the key array.
Does anyone know why this isn’t working? It’s my first time using meta_query but it seems to be correct from what I can find.
Thanks
Leave an answer