Get list of terms that have posts in another term

The question:

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
                                    )
                                )
                        ) );

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment