Is there a way to join WP_Query without a filter?

I have a CPT called service and a CPT called partner_request. The partner_request CPT will have post meta fields recipient_id and service_id and will have a custom post status of pending or accepted. I want to use WP_Query to retrieve a list of services that are partners with the service being viewed. The posts included in the results need to have a partner_request with a recipient_id or service_id equal to the original service’s ID and a post_status of accepted. I am aware that you can use add_filter(‘posts_join’) to add a join statement to a query. In my opinion, this looks sloppy and it would be easier to manage the code if I could use a join query within the WP_Query arguments, sort of in the same way you would use meta queries.

In the function I have that lets a user send a partner_request to another service, I make sure that no other partner_request posts between the services exist. Here is how I do that:

$request_atts = array(
‘post_type’ => ‘partner_request’,
‘post_status’ => array(‘pending’, ‘accepted’),
‘meta_query’ => array(
‘relation’ => ‘OR’,
array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘recipient_id’,
‘value’ => 94
),
array(
‘key’ => ‘service_id’,
‘value’ => 95
)
),
array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘recipient_id’,
‘value’ => 95
),
array(
‘key’ => ‘service_id’,
‘value’ => 94
)
)

)
);
$request_query = new WP_Query($request_atts);
if($request_query->have_posts()){
//A request already exists
} else {
//Send the request
}

So using the information above, is there a way to add a join clause to a WP_Query without filters?

$partner_atts = array(
‘post_type’ => ‘service’,
//Could a join that determines partners go here?
);

$partner_query = new WP_Query($partner_atts);
if($partner_query->have_posts()){
//Display a list of partners
}

Read more here:: Is there a way to join WP_Query without a filter?

Leave a Reply

Your email address will not be published. Required fields are marked *