最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

tags - Get all terms if product of that term contains a specific category

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Sep 15, 2016 at 0:20 James Vu 3,4193 gold badges25 silver badges45 bronze badges asked Sep 14, 2016 at 19:39 Meenakshi KhandelwalMeenakshi Khandelwal 377 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论