Advertisement
maxhodges0

relevanssi_hits_filter

Sep 4th, 2012
1,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1.  
  2.  
  3. /** Relevanssi customization by max **/
  4. add_filter('relevanssi_hits_filter', 'order_the_results');
  5.  
  6. function order_the_results($hits) {
  7.     global $wp_query;
  8.  
  9.     switch ($wp_query->query_vars['orderby']) {
  10.  
  11.         case 'likes':
  12.             $likes = array();
  13.             foreach ($hits[0] as $hit) {
  14.                 $likecount = get_post_meta($hit->ID, '_likes', true);
  15.             if (!isset($likes[$likecount])) $likes[$likecount] = array();
  16.             array_push($likes[$likecount], $hit);
  17.         }
  18.  
  19.         if ($wp_query->query_vars['order'] == 'asc') {
  20.           ksort($likes);
  21.         } else {
  22.           krsort($likes);
  23.         }
  24.      
  25.         $sorted_hits = array();
  26.         foreach ($likes as $likecount => $year_hits) {
  27.             $sorted_hits = array_merge($sorted_hits, $year_hits);
  28.         }
  29.         $hits[0] = $sorted_hits;
  30.         break;
  31.  
  32.     case 'comments':
  33.  
  34.         $comments = array();
  35.         foreach ($hits[0] as $hit) {
  36.             $commentcount = wp_count_comments($hit->ID)->total_comments;
  37.             if (!isset($comments[$commentcount])) $comments[$commentcount] = array();
  38.             array_push($comments[$commentcount], $hit);
  39.         }
  40.  
  41.         if ($wp_query->query_vars['order'] == 'asc') {
  42.             ksort($comments);
  43.             } else {
  44.             krsort($comments);
  45.         }
  46.  
  47.         $sorted_hits = array();
  48.         foreach ($comments as $commentcount => $year_hits) {
  49.             $sorted_hits = array_merge($sorted_hits, $year_hits);
  50.         }
  51.         $hits[0] = $sorted_hits;
  52.  
  53.     break;
  54.    
  55.     case 'relevance':
  56.         //do nothing
  57.         break;
  58.  
  59.      }
  60.  
  61.  
  62.     return $hits;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement