获取来自多个类别的所有产品(Woocommerce/Wordpress)
问题描述:
我想一次显示来自多个类别的所有产品。获取来自多个类别的所有产品(Woocommerce/Wordpress)
当我要显示所有产品从一类我的$ 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
我知道这是一件简单的事情,但我无法弄清楚为什么它不工作。 感谢您的帮助!
答
请尝试下面的代码段。
$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'
)
),
);
有点复杂,但感谢队友! –
不客气@B –