WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[resolved] Custom loop using multiple categories (2 posts)

  1. Dcone
    Member
    Posted 1 year ago #

    I'm trying to create a loop filtering by multiple categories.

    I have the two product category slugs saved as variables.

    Here are some loop args that work for me with just one category:

    <?php
    $args = array('post_type' => 'product', 'product_cat' => $catOne);
    ?>

    But how do I filter by two categories? I have tried the following with no luck:
    'product_cat' => array($catOne, $catTwo)

    Any help would be greatly appreciated, thanks!

    http://wordpress.org/extend/plugins/woocommerce/

  2. Dcone
    Member
    Posted 1 year ago #

    I was able to solve my problem using the following:

    <?php
    $args = array(
      'post_type' => 'product',
      'posts_per_page' => -1,
      'tax_query' => array(
        'relation' => 'AND',
        array(
         'taxonomy' => 'product_cat',
         'field' => 'slug',
         'terms' => $catOne
        ),
        array(
         'taxonomy' => 'product_cat',
         'field' => 'slug',
         'terms' => $catTwo
        )
      )
    );
    ?>

    Hope it helps someone else!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic