Hey. I want to get the number of products that belong to the category with id = * and which belong to taxonomies with id .... (maybe several). It is not necessary to receive their projects as there will be a lot of requests for which there will be a large load. It can be done in sql, namely select count (*), but I can’t make a database request, please help)

  • Product categories are taxonomies, only hierarchical ones are most often, and tags are not hierarchical. With the usual WP_Query, you can select all products, specifying which taxonomies you need to take these products from - specifically eugene_v
  • Or rather, from which specific terms of a particular taxonomy to choose - eugene_v

1 answer 1

You can take as an example a sample, as implemented in WooCommerce.

add_action( 'woocommerce_before_shop_loop', 'add_product_count_view', 10); function add_product_count_view() { $terms = get_the_terms( $post->ID,'product_cat' ); foreach( $terms as $term ) { echo 'Product Category: ' . $term->name . ' - Count: ' . $term->count; } } 

Here you get the number of products in each category.