I can’t help you much with the query to directly query the DB. That would be over my head. I could probably figure it out but I prefer easier solutions.
For something like this I would create an acf/save_post action. In my filter I would get the values from the related posts that I want to base a search on and store them in WP custom fields connected to the posts I want to search. Here is an only explanation of doing that https://web.archive.org/web/20190814230622/https://acfextras.com/dont-query-repeaters/. The link deals with repeater fields but it can easily be applied to this case as well.
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!
© 2023 Advanced Custom Fields.