Update 1
As @Florian pointed out, we do not need to use the wp_list_pluck function, we could simply add the 'fields' => 'ids'
to the WP_Query to retrieve the list of ids:
$producers = new WP_Query(
array(
'fields' => 'ids',
'post_type' => 'producers',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'region',
'field' => 'slug',
'terms' => 'alsace'
)
)
)
);
Then retrieve the wines by replacing $producerIds
by $producers->posts
.
Solution
First, I had to retrieve the producers from the region:
$producers = new WP_Query(
array(
'post_type' => 'producers',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'region',
'field' => 'slug',
'terms' => 'alsace'
)
)
)
);
Then I retrieve the list of ids with wp_list_pluck:
$producerIds = wp_list_pluck($producers->posts, 'ID');
And finally, I retrieve the wines with the ids of the producers:
$wines = new WP_Query(
array(
'post_type' => 'wines',
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'white'
)
),
'meta_query' => array(
array(
'key' => 'producer',
'value' => $producerIds,
'compare' => 'IN'
)
)
)
);