How to do a WP_Query when a post has a relationship to anoter post via an ACF Post Object field, where the related post is in a certain category?

I have custom post type A, and B.

B has a relationship back to A via an ACF Post Object field.

A has (custom) taxonomies that B does not.

How do you get all B where A is in taxonomy X.

I imagine how you would do this with SQL using an inner join, but wanted to see if its possible using a single WP_Query? I've only found examples where you are querying based on the related site id (https://www.advancedcustomfields.com/resources/querying-relationship-fields/).

Assuming this is not possible, would this be the most efficient query:

In this case B is offers, A is sites.

SELECT 
*
FROM `wp_posts` as offers
INNER JOIN `wp_postmeta` as
ON offers.id = meta.post_id
AND offers.post_type = 'offer'
INNER JOIN `wp_posts` AS sites
ON sites.id = meta.meta_value
INNER JOIN `wp_term_relationships` AS tr ON (sites.ID = tr.object_id)
WHERE tr.term_taxonomy_id IN ('4', '6') # ids of tax i want to filter by
AND meta.meta_key = 'site'
AND offers.post_status = 'publish'
GROUP BY offers.ID
HAVING COUNT(DISTINCT tr.term_taxonomy_id) = 2;

Without the group by having count i would get a row per matched taxonomy back, can't think how you would do this with in taxonomy 4 AND 6.

Topic advanced-custom-fields advanced-taxonomy-queries Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.