geminilabs

[site-reviews] Use absolute rating value in glsr_get_reviews()

Apr 14th, 2021 (edited)
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. /**
  2.  * Changes the rating parameter of the glsr_get_reviews function to use an absolute rating value
  3.  *
  4.  * @param array $and
  5.  * @param string $handle
  6.  * @param \GeminiLabs\SiteReviews\Database\Query $query
  7.  * @return array
  8.  * @version Site Reviews v5
  9.  */
  10. function glsr_use_absolute_rating ($and, $handle, $query) {
  11.     $filteredHandles = ['query-review-ids', 'query-total-reviews'];
  12.     if (in_array($handle, $filteredHandles) && !empty($and['rating'])) {
  13.         $and['rating'] = sprintf('AND r.rating = %d', $query->args['rating'] + 1);        
  14.     }
  15.     return $and;
  16. }
  17.  
  18. // add the filter before running glsr_get_reviews()
  19. add_filter('site-reviews/query/sql/and', 'glsr_use_absolute_rating', 11, 3);
  20.  
  21. $reviews = glsr_get_reviews(['rating' => 2]);
  22.  
  23. // remove the filter after running glsr_get_reviews()
  24. remove_filter('site-reviews/query/sql/and', 'glsr_use_absolute_rating', 11);
Add Comment
Please, Sign In to add comment