Search code examples
phpwordpresswoocommerceproductcustom-taxonomy

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

    <?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. Any idea why this is generating all products, instead of just the ones in the current category?


Solution

  • 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>';
    endwhile;
    
    wp_reset_postdata();
    

    Tested and works