[英]Woocommerce, get products by category with wp query
I tried many methods, but I can't show products with category name or id我尝试了很多方法,但我无法显示带有类别名称或 id 的产品
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'cat' => 40,
'posts_per_page' => '12',
);
$loop = new WP_Query( $args );
Try this example,试试这个例子,
So given a category with ID 26, the following code would return it's products (WooCommerce 3+):因此,给定 ID 为 26 的类别,以下代码将返回其产品(WooCommerce 3+):
$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => '12', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', //This is optional, as it defaults to 'term_id' 'terms' => 26, 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too 'operator' => 'NOT IN' ) ) ); $products = new WP_Query($args); var_dump($products);
Try tax_query
试试tax_query
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array('40'),
'operator' => 'IN',
)
)
);
$loop = new WP_Query( $args );
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
I suggest used >> wc_get_product_terms( $vars->ID, 'pa_brand_category' )我建议使用 >> wc_get_product_terms( $vars->ID, 'pa_brand_category' )
please check this screenshot >> http://prntscr.com/hjawu5请检查此屏幕截图 >> http://prntscr.com/hjawu5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.