Home › Forums › ACF PRO › query a relationship from another field in same post › Reply To: query a relationship from another field in same post
Hi @rudtek
I believe you can do it like this:
function my_post_object_query( $args, $field, $post_id ) {
// Get the allowed post IDs from the relationship field
$allowed_ids = get_field('event_speakers', $post_id, false);
// Restrict the returned posts
$args['post__in'] = $allowed_ids;
// return
return $args;
}
// filter for a specific field based on it's name
add_filter('acf/fields/post_object/query/name=session_speaker', 'my_post_object_query', 10, 3);
add_filter('acf/fields/post_object/query/name=session_moderator', 'my_post_object_query', 10, 3);
add_filter('acf/fields/post_object/query/name=session_emcee', 'my_post_object_query', 10, 3);
add_filter('acf/fields/post_object/query/name=session_host', 'my_post_object_query', 10, 3);
Please keep in mind that you need to save the post first after changing the speakers field.
I hope this helps 🙂
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.