Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 619 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
WP_Query Woocommerce products that belong in distinct multiple categories only tax_query

#1
I'm using `WP_Query` for Woocommerce products in attempt to query products in a particular category. This is the syntax that worked for me -

$args = array(
'posts_per_page' => -1,
'product_cat' => 'category-slug-here',
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '' . get_the_title() . '<br /><br />';
}
wp_reset_postdata();

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 **only**.

The argument `product_cat` is not native to `WP_Query` (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.

Using `cat`, the array of `tax_query`, and `category__and` 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.

Does anyone know how to accomplish this?

**UPDATE:** I have learned that separating category slugs by commas in the `product_cat` 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:

'product_cat' => 'category-slug1, category-slug2'

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.
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through