Skip to content
Advertisement

Get products with specific attribute term and specific category term in Woocommerce using SQL

I wish to find products with two different criteria.

The code I used first to search one criteria is;

This returns all those products (Object_ID) with the attribute “pa_1_scale” and ts.term_id = 400. I can also do this to return all products with product_cat and ts.term_id = 397, using a different WHERE statement

UNION ALL just combines the two. How do I get SQL to select both these criteria? I know a WHERE statement combining the two criteria will not work as I think that no table row contains both values?

Any help available would be great.

Advertisement

Answer

You can try to use the following that will join duplicated tables with a different variable reference, allowing to combine both queries in one:

Or you can use in WordPress the class WPDB and its methods to get SQL query results in PHP:

It should work.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement