Is there a way to query the data from another field in the same post?
i.e. I have a post called events. and this post has a field called ‘event_speakers’. This relationship field is querying another CPT called ‘speakers’. I can choose all the speakers i want to attend this event fine. The question is now this:
I have another field that’s called ‘event_session’ Is there any way to have this pull ONLY the speakers that have been chosen in the ‘event_speakers’ field? (some filter maybe?)
The other option would be to make the ‘event_speakers’ field be a repeater that has some fields, and then have the ‘event_session’ pull from that speaker list. (avoiding the “speakers” CPT altogether)
Hi @rudtek
I’m afraid I don’t understand your setup, so forgive me if I’m wrong. If you want to load the choices of a field dynamically from another field without reloading the page, then you need to use AJAX. This page should give you more idea about it: https://www.bobz.co/dynamically-populate-select-fields-choice-in-advanced-custom-fields/.
I’m afraid I can’t give you any advice regarding the repeater method because I don’t know your setup exactly. Could you please share some screenshots of it? Also, please share the JSON or XML export file of your field group too.
Thanks 🙂
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 🙂
The topic ‘query a relationship from another field in same post’ is closed to new replies.
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.