从 WP_Query 中的当前产品类别术语 ID 获取所有 Woocommerce 产品
Get all Woocommerce products from current product category term Id in a WP_Query
我有一个简单的分类法-product_cat.php 页面,我试图在其中仅显示当前类别中的产品。现在,它显示所有产品,而不仅仅是当前类别中的产品。这是我的代码:
<ul class="products">
<?php
$current_cat_id = $wp_query->get_queried_object()->term_id;
$args = array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $current_cat_id,
'operator' => 'IN'
)
);
$products = new WP_Query( $args );
while ( $products->have_posts() ) : $products->the_post();
echo '<li><a href="'. get_permalink() .'"><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></a></li>';
endwhile;
wp_reset_query();
?>
</ul>
我的客户一直在更改类别的 names/slugs,所以我需要通过类别 ID 获取产品。知道为什么这会生成所有产品,而不仅仅是当前类别中的产品吗?
更新 2
The tax query need to have 2 arrays embedded in each other: 'tax_query' => array( array(
删除了第一个不需要的'taxonomy' => 'product_cat',
已将 'terms' => $current_cat_id,
替换为 'terms' => array( get_queried_object()->term_id ),
替换了wp_reset_query();通过 wp_reset_postdata();
您的代码将是:
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( get_queried_object()->term_id ),
) )
) );
while ( $query->have_posts() ) : $query->the_post();
echo '<li><a href="'. get_permalink() .'"><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></a></li>';
endwhile;
wp_reset_postdata();
经过测试并有效
虽然它的正确答案你可以通过类别 slug 得到它。
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'category slug here',
);
$products_all = new WP_Query( $args );
测试版本 4.6.1
$terms = wp_get_post_terms( $product->id, 'product_cat' );
$args_listing = array(
'post_type' => 'product',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms[0]->term_id,
) ),
'orderby'=> 'date',
'order'=> "desc",
'posts_per_page' => 10,
'paged' => $paged
);
I think you can try this. its working for me.
我有一个简单的分类法-product_cat.php 页面,我试图在其中仅显示当前类别中的产品。现在,它显示所有产品,而不仅仅是当前类别中的产品。这是我的代码:
<ul class="products">
<?php
$current_cat_id = $wp_query->get_queried_object()->term_id;
$args = array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $current_cat_id,
'operator' => 'IN'
)
);
$products = new WP_Query( $args );
while ( $products->have_posts() ) : $products->the_post();
echo '<li><a href="'. get_permalink() .'"><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></a></li>';
endwhile;
wp_reset_query();
?>
</ul>
我的客户一直在更改类别的 names/slugs,所以我需要通过类别 ID 获取产品。知道为什么这会生成所有产品,而不仅仅是当前类别中的产品吗?
更新 2
The tax query need to have 2 arrays embedded in each other:
'tax_query' => array( array(
删除了第一个不需要的'taxonomy' => 'product_cat',
已将 'terms' => $current_cat_id,
替换为 'terms' => array( get_queried_object()->term_id ),
替换了wp_reset_query();通过 wp_reset_postdata();
您的代码将是:
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( get_queried_object()->term_id ),
) )
) );
while ( $query->have_posts() ) : $query->the_post();
echo '<li><a href="'. get_permalink() .'"><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></a></li>';
endwhile;
wp_reset_postdata();
经过测试并有效
虽然它的正确答案你可以通过类别 slug 得到它。
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'category slug here',
);
$products_all = new WP_Query( $args );
测试版本 4.6.1
$terms = wp_get_post_terms( $product->id, 'product_cat' );
$args_listing = array(
'post_type' => 'product',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms[0]->term_id,
) ),
'orderby'=> 'date',
'order'=> "desc",
'posts_per_page' => 10,
'paged' => $paged
);
I think you can try this. its working for me.