0

I don't know if I'm explaining this correctly, but I have a loop that's pulling all products assigned to a specific category (in this case, Current Season - this is for a performing arts organisation).

But each product is actually assigned to multiple categories for various reasons, and in this case I want all products assigned to 'current-season', but NOT also assigned to the category 'series'.

I've tried the following, but it has done nothing to change my query display.

It's still showing everything assigned to 'current-season'.

$args_right = array(
    'post_type' => 'product',
    'posts_per_page' => 999,
    //'product_cat' => 'current-season',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'series',
            'operator' => 'NOT IN'
        ),
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => 'current-season',
            'operator' => 'IN'
        )
    ),
    'meta_key' => 'date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

I'm sure I'm just missing something very obvious. I bang my head against the wall, and then wind up going "D'UH"!!

Thanks in advance!

kashalo
  • 3,442
  • 2
  • 11
  • 28
Laura Sage
  • 33
  • 7

1 Answers1

1

i beleive your problem with the 'meta_key' => 'date' if you want to order the posts by date you can use the following query.

$args_right =  array(
    'post_type' => 'product',
    'posts_per_page' => 12,
    'orderby' => 'date',
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'terms' => array('current-season'),
            'field' => 'slug',
            'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
        ), array(
            'taxonomy' => 'product_cat',
            'terms' => array('series'),
            'field' => 'slug',
            'operator' => 'NOT IN' // Possible values are 'IN', 'NOT IN', 'AND'.
        )
        ),

);


$loop = new WP_Query($args_right);
if ($loop->have_posts()) {
    while ($loop->have_posts()) :
        $loop->the_post();
        wc_get_template_part('content', 'product');
    endwhile;
} else {
    echo __('No products found');
}
wp_reset_postdata();

Reference

I tested this query locally and it's displaying all product with current-season category and if one product in this category assigned to another category or sub-category series it's excluded

kashalo
  • 3,442
  • 2
  • 11
  • 28
  • Thanks loads! The date ordering was working fine. But it would seem that just be reversing the order of the tax_query, that fixed my issue. Thank you SO much!! – Laura Sage Sep 12 '18 at 15:52
  • you are most welcome glade to hear that your issue is solved :) – kashalo Sep 12 '18 at 15:53