• Resolved bavington

    (@bavington)


    Hey guys, looking for some help excluding a single category from a custom product loop I’m doing. Essentially I have a called called ‘frames’ with ID ‘2018’. Any products assigned to this category, I wish to exclude within my loop.

    Here’s my arguments:

    $args = array(
    ‘post_type’ => ‘product’,
    ‘posts_per_page’ => 4,
    ‘orderby’ =>’date’,
    ‘order’ => ‘DESC’,
    ‘post_status’ => ‘publish’,
    ‘product_cat’ => ‘-frames’
    );
    $loop = new WP_Query( $args );

    I’m assuming that ‘product_cat’ is a custom parameter created by WooCommerce, and this can be used to specify a category to only get products from. You’ll notice I’m trying to exclude frames, but this doesn’t seem to work.

    Any help would be greatly appreciated, thanks, james.

    https://wordpress.org/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Thread Starter bavington

    (@bavington)

    Managed to solve this myself, with the tax_query parameter.

    $args = array(
    ‘post_type’ => ‘product’,
    ‘posts_per_page’ => 4,
    ‘orderby’ =>’date’,
    ‘order’ => ‘DESC’,
    ‘post_status’ => ‘publish’,
    ‘tax_query’ => array(
    array(
    ‘taxonomy’ => ‘product_cat’,
    ‘field’ => ‘slug’,
    ‘terms’ => ‘frames’
    ‘operator’ => ‘NOT IN’
    )
    )
    );
    $loop = new WP_Query( $args );

Viewing 1 replies (of 1 total)
  • The topic ‘How can I exclude a product_cat from a custom loop’ is closed to new replies.