0

I have a custom post type called 'products' the CPT has two custom taxonomies (both hierarchical); 1) 'product_type' and 2) 'product_provider'.

Each product will have both a 'product_type' and a 'product_provider' linked to it. On the archive page for each 'product_type' term I am outputting a filterable list of 'products', one of the filter options is by Provider which (obviously) filters based on the 'product_provider' custom tax.

The problem I have is outputting just the 'product_provider' terms that apply to the specific 'product_type'.

EXAMPLE

Product A. Product_type = 1, product_provider = W

Product B. Product_type = 1, product_provider = X

Product C. Product_type = 2, product_provider = Y

Product C. Product_type = 2, product_provider = W

On the archive page for 'product_type 1' using:

$terms = get_terms( array('taxonomy' => 'product_provider') );

Will output all terms within the 'product_provider' taxonomy - W, X, Y. I need to just output W and X - that is the 2 terms within 'product_provider' that apply to 'products' in the product_type custom tax.

I'm not sure if the answer is; a more robust term query, restructuring the CPT/taxonomies to remove the 'product_provider' - just make each provider a sub category of the specific 'product_type' (although this would lead to duplicating some providers as they offer products in multiple product_type's).

1 Answer 1

0

You can use another argument in the array of argument, you have many choice to filter the results.

You can add exclude, include with an array or comma separated of the terms you want to display.

$terms = get_terms( array(
            'taxonomy' => 'product_provider',
            'include'=> array('X', 'Y')
);

But, if the results depends on the product_type, you'll maybe need to use meta_key and meta_value (of the product).

Of course you can mix, all these arguments if need.

If you want to get the term that belong to a product, you need to use wp_get_object_terms().

You will find all details get_terms and wp_get_object_terms

Hope it helps.

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.