Hello everyone, I need to filter a relationship field by post object, I want the relationship field to show the posts that have the same id of post object field with the current post, I tried this code, but it’s not working. Can anyone tell me what’s wrong with this code?
function my_relationship_query( $args, $field, $post_id ) {
$ntry = the_field('manga_title');
$args['meta_query'] = array(array(
'key' => 'manga_title',
'value' => $ntry,
'compare' => '=',));
$args['meta_key'] = 'chapter_num';
$args['post_status'] = 'publish';
$args['orderby'] = 'meta_value';
$args['order'] = 'DESC';
// return
return $args;
}
add_filter('acf/fields/relationship/query/name=chapterslist', 'my_relationship_query', 10, 3);
What you’re trying to do is not clear. Can you give a better explanation?
I have a two post types, the first one is ‘chapters’, and the second id ‘novel’. in the’post’ post type I have a post object field that displays the novels of ‘novel’ post type. and I have a relationship field in the ‘chapters’ post type, and I want to filter the results of relationship field to display the chapters that have the same novel in post object field. I hope I explained my problem good.
There isn’t anything that will allow you to filter a relationship field based on a selection made in another field. The value of the other field is not available for the acf/fields/relationship/query/
filter, it is not supplied by ACF when it runs the AJAX request to get the posts.
There are a couple of possibilities.
1) is that you must save the post before you can filter based on this other field. Then in your filter that you posted above
'value' => get_field('filter_field_name', $post_id)
2) Do not use a relationship field and switch to a select field and add custom JavaScript and your own AJAX query to get the posts. There are some examples of this here https://github.com/Hube2/acf-dynamic-ajax-select-example
Thank you! That was very useful.