2

I have a custom post type Wine, which has an ACF Field that links to another custom post type Producer as a post_object relation. In my Producer, I have a custom taxonomy named Region.

Now, what I'm trying to do is to query all the white wines where the producer's region is x.

Here's what I've tried:

$args = array(
    'post_type' => 'wines',
    'posts_per_page' => 12,
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'white'
        ),
        array(
            'taxonomy' => 'region',
            'field' => 'slug',
            'terms' => 'alsace'
        )
    )
);

The problem I'm having is the tax_query is executed on the wine and not it's producer. I've looked at the documentation of WP_Query and can't seem to find an example on how to achieve it.

Could anyone please guide me? Thanks.

Update 1

I've retrieved the list of IDs of the producers to add it to the meta_query:

$producers = new WP_Query(
    array(
        'post_type' => 'producers',
        'posts_per_page' => -1
    )
);

$producersIds = wp_list_pluck($producers, 'ID');

After that, I've updated the $args by adding the meta query:

'meta_query' => array(
    array(
        'key' => 'producer',
        'value' => $producerIds,
        'compare' => 'IN'
    )
)
4
  • 1
    It sounds like the only taxonomy that you technically have linked is the type of wine. The region is tied through a post object, which would be stored through the meta (ACF), usually as a simple post ID. So, you might have to request all Producers from the "Alsace" region first. Then, add those IDs to a meta_query, while still using the type under the tax_query. Hopefully I followed your logic.
    – socki03
    Commented Nov 1, 2016 at 14:43
  • @socki03 Yes, that's my logic. I'll try it and come back to you! Thanks!
    – Chin Leung
    Commented Nov 1, 2016 at 14:46
  • 1
    It looks like you're getting all producers, shouldn't you be only getting the ones from the Alsace (or equivalent) region? You should just be able to add 'region' => 'alsace' to the producers query args.
    – socki03
    Commented Nov 1, 2016 at 14:58
  • @socki03 Yeah that's what I've figured after updating my question! I got it working now! I'll post an answer for users who has the same issues later! Thank you!
    – Chin Leung
    Commented Nov 1, 2016 at 15:01

1 Answer 1

3

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'
            )
        )
    )
);
4
  • 2
    Had never heard of this wp function, wp_list_pluck... very useful.
    – gdaniel
    Commented Nov 1, 2016 at 15:12
  • 1
    2 things (just for clarification), shouldn't the tax query have another array wrapped around it, similar to the meta_query? (array of arrays), OR you could simplify the tax_query by just rewriting it as a simple custom taxonomy request by using the slug of your taxonomy and assigning either a simple slug or array of slugs.
    – socki03
    Commented Nov 1, 2016 at 15:17
  • @socki03 Oops! Yes there's an array wrapping in the tax_query!
    – Chin Leung
    Commented Nov 1, 2016 at 15:38
  • 2
    You don't need wp_list_pluck if you only need the ids (although it is of course a super nifty function). You can also use the 'fields'=>'ids' parameter to return an array of ids ($producers->posts). codex.wordpress.org/Class_Reference/…
    – Florian
    Commented Nov 1, 2016 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.