1

I have a product attribute called "color" and when I am on the archive page (e.g. color=blue) I want to display a list of all the parent product categories that contain products with that attribute, color=blue.

I tried using get_queried_object_id() to get the archive term (blue), in conjunction with get_terms(), however I couldn't figure it out. I'd like to reiterate that I want to retrieve the list of terms and not the posts.

If anyone can lead me in the right direction, I'd appreciate it!

This question seems similar to what I want, however it uses wpdb, and want to use a regular query.

I was tried doing something like this, to get a list of all posts that are in both taxonomies (pa_color and product category), but I am not sure how to get a list of just categories.

$current_color = get_queried_object_id();

$query = new WP_Query( array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'orderby'        => 'title',
    'order'          => 'ASC',
    'tax_query'      => array(
                               array(
                                        'taxonomy' => 'pa_color',
                                        'field'    => 'term_id',
                                        'terms'    => $current_color,
                                        'operator' => 'AND'
                                    ),
                                    array(
                                        'taxonomy' => 'product_cat',
                                        'field'    => 'term_id',
                                        'terms'    => $cats
                                    )
                                )
                        ) );
2
  • Paste a code that you used
    – anton
    Commented May 24, 2021 at 10:15
  • @anton Iadded some code to the post, however I couldn't get anything to work properly.
    – Edegist
    Commented May 24, 2021 at 18:40

1 Answer 1

4

Try replacing 'operator' => 'AND' with 'relation'=>'AND'

Updated Code Snippet:

$current_color = get_queried_object_id();

$query = new WP_Query( array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'orderby'        => 'title',
    'order'          => 'ASC',
    'tax_query'      => array(
                              'relation' => 'AND'
                               array(
                                        'taxonomy' => 'pa_color',
                                        'field'    => 'term_id',
                                        'terms'    => $current_color,
                                    ),
                                    array(
                                        'taxonomy' => 'product_cat',
                                        'field'    => 'term_id',
                                        'terms'    => $cats
                                    )
                                )
                        ) );

Reference : https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters ( Multiple Taxonomy Handling )

Edit 1: You can edit above query to return only ids ( 'fields' => 'ids' ) and use get_terms for those IDs to get the list of categories

get_terms( array( 
   'taxonomy' => 'product_cat',
   'object_ids' => $posts_matching_criteria
); 

Ref 1 : https://developer.wordpress.org/reference/functions/get_terms/

Ref 2 : https://developer.wordpress.org/reference/classes/wp_term_query/__construct/#user-contributed-notes

2
  • Thanks @ViralP, however, won't this just output the POSTS that have these 2 taxonomies in common? I need it to output a list of product categories that contain products with the particular color.
    – Edegist
    Commented May 26, 2021 at 5:00
  • 1
    @Edegist : If I have understood the requirement properly, I think the above WP_Query returns the required posts and to get the categories you can use 'get_terms' -- Updated answer with example and links
    – ViralP
    Commented May 26, 2021 at 9:30

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.