Note that there are some explanatory texts on larger screens.

plurals
  1. POWP_Query Woocommerce products that belong in distinct multiple categories only tax_query
    primarykey
    data
    text
    <p>I'm using <code>WP_Query</code> for Woocommerce products in attempt to query products in a particular category. This is the syntax that worked for me -</p> <pre><code>$args = array( 'posts_per_page' =&gt; -1, 'product_cat' =&gt; 'category-slug-here', 'post_type' =&gt; 'product', 'orderby' =&gt; 'title', ); $the_query = new WP_Query( $args ); // The Loop while ( $the_query-&gt;have_posts() ) { $the_query-&gt;the_post(); echo '' . get_the_title() . '&lt;br /&gt;&lt;br /&gt;'; } wp_reset_postdata(); </code></pre> <p>This returns data, but I want to pass an ID, not a category slug, to filter and I want to find products that exist in multiple categories <strong>only</strong>. </p> <p>The argument <code>product_cat</code> is not native to <code>WP_Query</code> (at least that I can find), so I'm assuming this is something custom to Woocommerce. Through their documentation, I haven't been able to find anything that will allow me to filter by category ID, nor use an AND condition for this filtering.</p> <p>Using <code>cat</code>, the array of <code>tax_query</code>, and <code>category__and</code> have not yielded any results. Essentially, I would like to query all products that exist in both category ID 102, and 115. If I have to use slugs, I'm sure there is a way around getting that info based on the ID I have, but I'd like to avoid 2 queries to filter by multiple categories.</p> <p>Does anyone know how to accomplish this?</p> <p><strong>UPDATE:</strong> I have learned that separating category slugs by commas in the <code>product_cat</code> argument will produce an "OR" effect, so it will combine distinct products from both, but this is not what I am looking for. So, for example:</p> <pre><code> 'product_cat' =&gt; 'category-slug1, category-slug2' </code></pre> <p>will return products from both categories in total, but I am still searching for a way to find distinct products that ONLY belong to both, or multiple, categories. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload