cmoreira

ttshowcase custom shortcode

Dec 17th, 2015
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. //Funtion to retrieve a message with the ratings of a particular taxonomy
  2. add_shortcode( 'show-testimonials-custom', 'ttshowcase_get_reviews' );
  3. function ttshowcase_get_reviews($atts) {
  4.  
  5. $tsargs = array(
  6. 'post_type' => 'ttshowcase',
  7. 'tax_query' => array(
  8. array(
  9. 'taxonomy' => 'ttshowcase_groups',
  10. 'field' => 'slug',
  11. 'terms' => $atts['slug'],
  12. ),
  13. ),
  14.  
  15. );
  16.  
  17. //perform the query
  18. $tts_query = new WP_Query( $tsargs );
  19.  
  20. //store the ratings in an array
  21. $collection = array(
  22.  
  23. '0' => 0,
  24. '1' => 0,
  25. '2' => 0,
  26. '3' => 0,
  27. '4' => 0,
  28. '5' => 0
  29.  
  30. );
  31.  
  32. $total = 0;
  33.  
  34. while ( $tts_query->have_posts() ) : $tts_query->the_post();
  35.  
  36. //get rating value;
  37. $rating = get_post_meta( get_the_ID(), '_aditional_info_rating', true );
  38.  
  39. //add it to collection array
  40. $collection[$rating] = $collection[$rating]+1;
  41.  
  42.  
  43. $total++;
  44.  
  45.  
  46. endwhile;
  47.  
  48. $output = '';
  49.  
  50. foreach ($collection as $key => $value) {
  51. //show only if there were ratings with that value
  52. if($value>0) {
  53. //build output
  54. $output .= 'Rating '.$key.' = '.$value.' times <br>';
  55. }
  56.  
  57. }
  58.  
  59. //add total rating count
  60. $output .= 'Total ratings:'.$total;
  61.  
  62. return $output;
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment