Querying WooCommerce Products by Category + Tag

In a recent Business Bloomer Club discussion, a member sought guidance on refining a WooCommerce product query. Initially, they successfully used WP_Query to fetch products but wanted to add filtering based on specific taxonomies, specifically the “3c” category and the “apple” tag.

Members suggested alternative methods, including the WooCommerce-specific wc_get_products function, which offers a streamlined approach for querying products.

Here, we’ll explore how to use both WP_Query and wc_get_products to filter products by category and custom labels.

Using WP_Query for Product Filtering

While WP_Query is flexible and widely used in WordPress, filtering products by category and custom labels requires specifying taxonomy parameters.

Code Example

$params = array(
    'posts_per_page' => 5,
    'post_type' => 'product',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat', // WooCommerce category taxonomy
            'field'    => 'slug',
            'terms'    => '3c', // Replace with your category slug
        ),
        array(
            'taxonomy' => 'product_tag', // WooCommerce tag taxonomy
            'field'    => 'slug',
            'terms'    => 'apple', // Replace with your tag slug
        ),
    ),
);

$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
    while ($wc_query->have_posts()) :
        $wc_query->the_post();
        ?>
        <div><?php the_title(); ?></div>
        <?php
    endwhile;
    wp_reset_postdata();
else: ?>
    <p><?php _e( 'No Products' );?></p>    
<?php endif;
?>

In this setup:

  • tax_query: Specifies filters based on the product_cat and product_tag taxonomies, targeting the desired category and label.

Using wc_get_products for WooCommerce-Specific Queries

WooCommerce offers wc_get_products, a function that simplifies product queries and is optimized for WooCommerce data. This method is recommended for complex queries as it provides better performance and flexibility.

Code Example

$params = array(
    'limit'     => 5,
    'category'  => array('3c'), // Category slug
    'tag'       => array('apple'), // Tag slug
);

$products = wc_get_products($params);
if ( ! empty($products) ) :
    foreach ( $products as $product ) :
        ?>
        <div><?php echo $product->get_name(); ?></div>
        <?php
    endforeach;
else: ?>
    <p><?php _e( 'No Products' ); ?></p>
<?php endif;
?>

In this setup:

  • category and tag: Parameters for filtering by specific WooCommerce category and tag slugs.

Final Thoughts

While both WP_Query and wc_get_products can achieve the same result, wc_get_products is preferred for WooCommerce stores due to its WooCommerce-optimized nature. By using these methods, you can efficiently filter products based on categories, tags, and other custom taxonomies, helping users find what they’re looking for faster.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

Your email address will not be published. Required fields are marked *