2017-03-03 12 views
0

複数のカテゴリのすべての商品を一度に表示したいと考えています。複数のカテゴリからすべての商品を取得する(Woocommerce/Wordpress)

は、私は1つのカテゴリからすべての製品を表示したい場合は、私の$ args配列は次のようになります。

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

私は単に私の$ argsを内部の配列を作ることができることを覚えておいてください:

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

しかし、それは私に次のエラーを与える:

Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\live\wp-includes\formatting.php on line 4312

私はこれは簡単なことを知っている動いていない。 助けてくれてありがとう!

答えて

1

以下のスニペットを試してください。

$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); 
+0

ビット複雑ですが、おかげでチームメイト行うための簡単な方法を発見します! –

+0

あなたはようこそ@B –

1

は、それが

$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' 
    ) 
), 
); 
関連する問題