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. Right now, it's displaying all products, not just the ones in the current category. Here is my code:

<ul class="products">

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


My client keeps changing the names/slugs of the category, so I need to get the products by category ID. Any idea why this is generating all products, instead of just the ones in the current category?


Update 2

The tax query need to have 2 arrays embedded in each other: 'tax_query' => array( array(

Removed the first unneeded 'taxonomy' => 'product_cat',

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

Replaced wp_reset_query(); by 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>';


Tested and works

Collected from the Internet

Please contact [email protected].com to delete if infringement.

edited at


Login to comment


Get a list of siblings term ids from current product category in WooCommerce

Get all WooCommerce products from a category that have specific meta data

Get WooCommerce featured products in a WP_Query

Get WooCommerce Subscription product type and specific category in a WP_Query

Product variation WP_Query with a product category in Woocommerce

Display products from specific product category in Woocommerce

Get sku from Woocommerce product bookings in a WP_Query

Exclude a WooCommerce product category from a WP_Query

Woocommerce, get products by category with wp query

Get Woocommerce Product Categories from Product variations in a WP_Query loop

Exclude Woocommerce products in recently viewed products widget from a product category

Exclude a product category from Woocommerce related products

Exclude a product category from Related products in WooCommerce

Get the subcategories of the current product category in Woocommerce archives

WP_Query get post by category id returns all categorries

How to get all products from current WooCommerce product category?

Get the category name from a WooCommerce product using the product id or the category id

Get also WooCommerce product category thumbnail Id using a SQL query

Get the id from a product custom field with linked products in WooCommerce

Get only one product category term for a WooCommerce product

Get products from a specific brand in WooCommerce using a WP_Query

Get manufacturers of all products in a category from non-product page

WooCommerce Delete all products from cart and add current product to cart

Magento - Get all products in the current category

woocommerce get product category id

How to get woocommerce products of current language with WP_Query?

Getting the 'thumbnail ID' from last product category term in WooCommerce

Get on sale products for Woocommerce product category archives using a Get request

WooCommerce database query : Get product category image path+name from thumbnail_id