Your query seem to fall outside of realm what’s “normal” for WP APIs to do.
There are two general approaches to it:
- Keep it completely in SQL. If you can figure out how to adapt it to WP’s schema you can use
$wpdb->get_results()
to run arbitrary query, get what you need, and proceed from there. - Doing that via
WP_Query
is more challenging since custom field parameters don’t operate with more complex math/logic like that. You would have to use WP’s filters to make changes to SQL it generates on the fly.
Overall I would say option 2 is something that makes sense to a developer more experienced with WP specific.
If you are comfortable with general PHP and SQL, but not as much with WP then just going to 1 will probably get things done faster for starters.