【问题标题】:Wordpress & woocommerce get products by product_category and partial SKU matchWordpress 和 woocommerce 按 product_category 和部分 SKU 匹配获取产品
【发布时间】:2022-07-19 20:44:53
【问题描述】:

我想返回属于某组 product_categories 的产品列表,并且还有一个以某种字符串模式开头的 SKU,到目前为止,我有以下内容,但它似乎忽略了 @987654322 @

$args = [
        'status' => 'publish',
        'ignore_sticky_posts' => 1,
        'meta_query' => [
            'key' => '_sku',
            'value' => '^RE-',
            'compare' => 'REGEXP'
        ],
        'tax_query' => [
            'relation' => 'AND',
            [
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => $cat_id,
            ],
            [
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => 789,
            ]
        ]
    ];

    $query = new WC_Product_Query($args);
    $products = $query->get_products();
    die(print_r($products));

我从tax_query 获得产品,但它似乎忽略了meta_query,因为正在退回任何 SKU?

【问题讨论】:

  • 即使你只有一组元条件,你仍然需要让meta_query成为一个数组内的数组,而不仅仅是一个数组。与 tax_query 使用的格式相同,仅包含一组条件,也不需要 relation
  • developer.wordpress.org/reference/classes/wp_query/…: "(请注意,meta_query 需要嵌套数组,即使您只有一个查询。)"

标签: php wordpress


【解决方案1】:

因此,如果我正确理解了帖子和 cmets,那么使用 relation 毫无意义。我说的对吗?

【讨论】:

  • 这真的是评论,而不是答案。多一点代表,you will be able to post comments。目前我已经为你添加了评论,并且我将这个帖子标记为删除。
猜你喜欢
  • 2016-03-04
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多