Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从多个类别获取所有产品(Woocommerce/Wordpress)_Php_Arrays_Wordpress_Woocommerce_Args - Fatal编程技术网

Php 从多个类别获取所有产品(Woocommerce/Wordpress)

Php 从多个类别获取所有产品(Woocommerce/Wordpress),php,arrays,wordpress,woocommerce,args,Php,Arrays,Wordpress,Woocommerce,Args,我想一次显示多个类别的所有产品 当我想显示一个类别中的所有产品时,我的$args数组如下所示: $args = array( 'post_type' => 'product', 'product_cat' => 'backpacks', 'orderby' => '_sku' ); 我记得我可以简单地在$args中创建一个数组: $args = array( 'post_type' => 'product', 'product_cat' =

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

当我想显示一个类别中的所有产品时,我的$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为字符串,数组在第4312行的C:\xampp\htdocs\live\wp includes\formatting.php中给出

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

请尝试下面的代码片段

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

请尝试下面的代码片段

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

找到了一个简单的方法

$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'
    )
),
);

找到了一个简单的方法

$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'
    )
),
);