I have 2 custom taxonomies:

  1. catalog (hierarchical)
  2. brand (not hierarchical)

and a custom post type solutions.

We assume that we have such a structure:

---solutions_1 (with brand_1, brand_2, brand_3)
---solutions_2 (with brand_1, brand_3, brand_4)
---solutions_3 (with brand_1, brand_2, brand_6)
---solutions_1 (with brand_1, brand_2, brand_3)
---solutions_4 (with brand_1, brand_4, brand_5)
---solutions_5 (with brand_7)

Who then has ideas how to get list of the unique brand terms for solutions in sub_category_1 and sub_category_2

like if we on page
sub_category_1 - list must be: brand_1, brand_2, brand_3, brand_4, brand_6
sub_category_2 - list must be: brand_1, brand_2, brand_3, brand_4, brand_5, brand_7


Assuming that when you are on the "page" sub_category_1 you're looking at a standard archive for that term, you can do this...

The main query is already for the posts in sub_category_1 so get a list of their IDs:

global $wp_query;
$post_ids = wp_list_pluck( $wp_query->posts, 'ID' );

Then you simply need to get a list of the terms from brand which are assigned to those posts:

$terms = get_terms( array(
    'taxonomy' => 'brand',
    'object_ids' => $post_ids,
) );

$terms will be an array of the WP_Term objects.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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