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 🙂
This reply has been marked as private.
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 🙂