首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Woocommerce wc_get_products() $args --如何显示指定了“所有”标记的帖子?

Woocommerce wc_get_products() $args --如何显示指定了“所有”标记的帖子?
EN

Stack Overflow用户
提问于 2019-11-19 07:13:52
回答 1查看 402关注 0票数 1

我需要wc_get_products()按标签选择产品,但只选择指定了所有标签的产品。

示例:

产品A有产品标签TagXTagY__。

产品B有产品标签TagX__。

如果我设置:

代码语言:javascript
复制
$args = array(
        'tag' => array('TagX', 'TagY')
);

wc_get_products()返回Product Product ,但我希望它在本例中只返回Product

有什么解决办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-19 07:36:54

请试一试--我还没有测试过它,但是我相信你应该知道我想传达的信息,

代码语言:javascript
复制
$args = array(
    'tag'      => array('TagX', 'TagY'),
    'return'   => 'objects'
);
$list_of_woocommerce_products = wc_get_products($args);
$required_array_of_objects = array();
$tag = get_term_by('slug', 'TagX');
$tag_id_X =  $tag->term_id;
$tag = get_term_by('slug', 'TagY');
$tag_id_Y =  $tag->term_id;
foreach($list_of_woocommerce_products as $single_woocommerce_product){
  if( in_array($tag_id_X, $single_woocommerce_product->get_tag_ids()) && in_array($tag_id_X, $single_woocommerce_product->get_tag_ids()) ){
        array_push($required_array_of_objects,$single_woocommerce_product);
     }
    }
//Required array of Woocommerce products having tags X and Y
print_r($required_array_of_objects);

请研究和分析上述untested-code.在得到基本想法后做出适当的修改,并将结果告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58928476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档