Support

Account

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 🙂