1

I am using below code for get all product tags:

$tags = get_terms('product_tag', 'orderby=name&hide_empty=1');

However, I want to show only those tags in which product having these tags but must contain a specific product category like shop.

Mean if a product1 contains tag1 and shop category product2 contains tag2 and any other category, then only tag1 will show.

1 Answer 1

1

I needed something similar. This is what I did:

$args = array(
        'post_status' => 'publish',
        'tax_query' => array(
            array(
                'taxonomy' => get_query_var('taxonomy'),
                'field' => 'slug',
                'terms' => get_query_var('term'),
            )
        ),
    );
    $products = new WP_Query($args);
    $attributes = array();
    while ($products->have_posts()) : $products->the_post();
        $post_atts = get_the_terms(get_the_ID(), 'pa_' . $attr);
        if ($post_atts) {
            foreach ($post_atts as $x) {
                if(!in_array($x, $attributes)){
                    $attributes[]=$x;
                }
            }
        }
    endwhile;
    print_r($attributes1);

This code is written for product and attributes, so you'll have to modify it for your need.

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.