1

I'm using the following query to extract all products which are not in the 'Service' product category and order them by a custom meta field '_vintage_age'.

$q->set( 'tax_query', array(array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array( 'Service' ),  
    'operator' => 'NOT IN'
)));

$q->set('meta_query',array(array(
    'vtp_age' => array(
    'key' => '_vintage_age',
    'value' => array(1,2,3),
    'compare' => 'IN'
))));

$q->set('orderby',array(
    'vtp_age' => 'ASC',
    'date' => 'ASC'
));

I now need to extend this query to EXCLUDE products which have been modified with the product_category 'Sold' more than 1 month ago (ie. display all unsold, non serviced products and all products sold in the last month). My guess is to use something like this, but how do I apply this to 'Sold' products only AND add it to my original query?

array(
    'date_query' => array(
        array(
            'column' => 'post_modified_gmt',
            'after'  => '1 month ago',
        ),
))

Thanks in advance for any help!

2

I'm afraid that there is no way to do such advanced queries using WP_Query.

But, of course, you can still achieve that result, but you'll need to write a little bit more code.

There are two solutions:

It's hard to give you precise code, because there aren't many details in your question, but maybe this will be enough :)

1. Use posts_where and posts_join filters

If you're not afraid of writing your own SQL queries, then you can use posts_where and posts_join filters to add some more advanced SQL conditions to your WP_Query.

2. Use second WP_Query and then exclude these posts

If you don't like raw SQL, then you can use second WP_Query. Use it to select all posts that you want to exclude. Then you can use their IDs and pass them as post__not_in param to your current WP_Query - this way you will exclude them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.