Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // get the cats this product is in
- $terms = wp_get_post_terms($post->ID, 'product_cat');
- $cats = array();
- $total = array();
- // if there is only one category jump out.
- if (count($terms) == 1) {
- $cats = $terms[0]->term_id;
- }else{
- // remove anything that is a parent cat
- foreach ($terms as $k => $term) {
- if ($term->parent === 0) {
- unset($terms[$k]);
- } else {
- // build list of terms we do want (children)
- $cats[] = $term->term_id;
- }
- }
- }
- foreach ($terms as $k => $term) {
- $total[] = $term->count;
- }
- // wp_reset_postdata();
- $related_products = new WP_Query(
- array(
- 'posts_per_page' => 3
- 'order' => 'DESC',
- 'orderby' => 'rand',
- 'post_type' => 'product',
- 'post__not_in' => $post->ID,
- 'tax_query' => array(
- array(
- 'taxonomy' => 'product_cat',
- 'terms' => $cats,
- )
- ),
- )
- );
- echo "<pre>";
- var_dump($cats);
- echo "</pre>";
- echo 'Total products with these terms: '.array_sum($total);
- echo "<pre>";
- $total = $related_products->post_count;
- echo 'Total in query: ';
- var_dump($total);
- // var_dump($related_products);
- echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement