简体   繁体   English

从 WP_Query 中的当前产品类别术语 ID 获取所有 Woocommerce 产品

[英]Get all Woocommerce products from current product category term Id in a WP_Query

I have a simple taxonomy-product_cat.php page, where I am trying to display only the products in the current category.我有一个简单的 taxonomy-product_cat.php 页面,我试图在其中仅显示当前类别中的产品。 Right now, it's displaying all products, not just the ones in the current category.现在,它显示所有产品,而不仅仅是当前类别中的产品。 Here is my code:这是我的代码:

<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>

My client keeps changing the names/slugs of the category, so I need to get the products by category ID.我的客户不断更改类别的名称/别名,因此我需要按类别 ID 获取产品。 Any idea why this is generating all products, instead of just the ones in the current category?知道为什么这会生成所有产品,而不仅仅是当前类别中的产品吗?

Update 2更新 2

The tax query need to have 2 arrays embedded in each other: 'tax_query' => array( array(税务查询需要有 2 个相互嵌入的数组: 'tax_query' => array( array(

Removed the first unneeded 'taxonomy' => 'product_cat',删除了第一个不需要的'taxonomy' => 'product_cat',

Replaced 'terms' => $current_cat_id, by 'terms' => array( get_queried_object()->term_id ),替换'terms' => $current_cat_id,'terms' => array( get_queried_object()->term_id ),

Replaced wp_reset_query();替换 wp_reset_query(); by wp_reset_postdata();通过wp_reset_postdata();

Your code will be:您的代码将是:

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

Tested and works测试和工作

although its correct answer you can get it through category slug.虽然它的正确答案你可以通过类别slug得到它。

    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 10,
        'product_cat'    => 'category slug here',
    );
    $products_all = new WP_Query( $args );

version tested 4.6.1测试版本 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM