【问题标题】:Get all products from multiple categories (Woocommerce/Wordpress)获取多个类别的所有产品(Woocommerce/Wordpress)
【发布时间】:2017-03-03 10:15:34
【问题描述】:

我想一次显示多个类别的所有产品。

当我想显示一个类别的所有产品时,我的 $args 数组如下所示:

$args = array(
   'post_type' => 'product',
   'product_cat' => 'backpacks',
   'orderby' => '_sku'
);

我记得我可以简单地在我的 $args 中创建一个数组:

$args = array(
   'post_type' => 'product',
   'product_cat' => array(
      'backpacks','accessoires',
),
   'orderby' => '_sku'
);

但它给了我以下错误:

警告:urlencode() 期望参数 1 为字符串,数组在 C:\xampp\htdocs\live\wp-includes\formatting.php 第 4312 行给出

我知道这是一件简单的事情,但我不知道为什么它不起作用。 感谢您的帮助!

【问题讨论】:

    标签: php arrays wordpress woocommerce args


    【解决方案1】:

    请尝试下面的sn-p。

    $sortcolumn = 'ID';
    $prod_categories = array(12, 17); //category IDs
    $product_args = array(
        'numberposts' => -1,
        'post_status' => array('publish', 'pending', 'private', 'draft'),
        'post_type' => array('product', 'product_variation'), //skip types
        'orderby' => $sortcolumn,
        'order' => 'ASC',
    );
    
    if (!empty($prod_categories)) {
        $product_args['tax_query'] = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => $prod_categories,
                'operator' => 'IN',
        ));
    }
    
    $products = get_posts($product_args);
    

    【讨论】:

      【解决方案2】:

      找到了一个简单的方法

      $args = array(
      'post_type' => 'product',
      'tax_query' => array(
          'relation' => 'OR',
          array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => 'backpacks'
          ),
          array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => 'accessoires'
          )
      ),
      );
      

      【讨论】:

        猜你喜欢
        • 2021-08-01
        • 2014-01-27
        • 2019-09-03
        • 2017-04-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多