Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. // get the cats this product is in
  2. $terms = wp_get_post_terms($post->ID, 'product_cat');
  3.  
  4.  
  5. $cats = array();
  6. $total = array();
  7. // if there is only one category jump out.
  8. if (count($terms) == 1) {
  9. $cats = $terms[0]->term_id;
  10. }else{
  11. // remove anything that is a parent cat
  12. foreach ($terms as $k => $term) {
  13. if ($term->parent === 0) {
  14. unset($terms[$k]);
  15. } else {
  16. // build list of terms we do want (children)
  17. $cats[] = $term->term_id;
  18. }
  19. }
  20. }
  21. foreach ($terms as $k => $term) {
  22. $total[] = $term->count;
  23. }
  24. // wp_reset_postdata();
  25.  
  26.  
  27. $related_products = new WP_Query(
  28. array(
  29. 'posts_per_page' => 3
  30. 'order' => 'DESC',
  31. 'orderby' => 'rand',
  32. 'post_type' => 'product',
  33. 'post__not_in' => $post->ID,
  34. 'tax_query' => array(
  35. array(
  36. 'taxonomy' => 'product_cat',
  37. 'terms' => $cats,
  38. )
  39. ),
  40. )
  41. );
  42.  
  43. echo "<pre>";
  44. var_dump($cats);
  45. echo "</pre>";
  46.  
  47. echo 'Total products with these terms: '.array_sum($total);
  48.  
  49. echo "<pre>";
  50. $total = $related_products->post_count;
  51. echo 'Total in query: ';
  52. var_dump($total);
  53.  
  54. // var_dump($related_products);
  55. echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement