SHARE
TWEET

Untitled

a guest Mar 16th, 2016 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_filter('relevanssi_hits_filter', 'order_the_results');
  2. function order_the_results($hits) {
  3.     global $wp_query;
  4.  
  5.     switch ($wp_query->query_vars['orderby']) {
  6.         case 'evaluation': //name of my meta_key
  7.             $eval = array();
  8.             foreach ($hits[0] as $hit) {
  9.                 $evalcount = get_post_meta($hit->ID, '_evaluation', true);
  10.                 if (!isset($eval[$evalcount])) $eval[$evalcount] = array();
  11.                         array_push($eval[$evalcount], $hit);
  12.                 }
  13.  
  14.             if ($wp_query->query_vars['order'] == 'desc') {
  15.                 ksort($eval);
  16.             } else {
  17.                 krsort($eval);
  18.             }
  19.  
  20.                 $sorted_hits = array();
  21.             foreach ($eval as $evalcount => $year_hits) {
  22.                 $sorted_hits = array_merge($sorted_hits, $year_hits);
  23.             }
  24.         $hits[0] = $sorted_hits;
  25.        
  26.         break;
  27.  
  28.     case 'rate':
  29.             $rate = array();
  30.             foreach ($hits[0] as $hit) {
  31.                 $ratecount = get_post_meta($hit->ID, '_rate', true);
  32.                 if (!isset($rate[$ratecount])) $rate[$ratecount] = array();
  33.                         array_push($rate[$ratecount], $hit);
  34.                 }
  35.  
  36.             if ($wp_query->query_vars['order'] == 'desc') {
  37.                 ksort($rate);
  38.             } else {
  39.                 krsort($rate);
  40.             }
  41.  
  42.                 $sorted_hits = array();
  43.             foreach ($rate as $ratecount => $year_hits) {
  44.                 $sorted_hits = array_merge($sorted_hits, $year_hits);
  45.             }
  46.         $hits[0] = $sorted_hits;
  47.        
  48.         break;
  49.  
  50.         case 'price':
  51.             $price = array();
  52.             foreach ($hits[0] as $hit) {
  53.                 $pricecount = get_post_meta($hit->ID, '_price', true);
  54.                 if (!isset($price[$pricecount])) $price[$pricecount] = array();
  55.                         array_push($price[$pricecount], $hit);
  56.                 }
  57.  
  58.             if ($wp_query->query_vars['order'] == 'asc') {
  59.                 ksort($price);
  60.             } else {
  61.                 krsort($price);
  62.             }
  63.  
  64.                 $sorted_hits = array();
  65.             foreach ($price as $pricecount => $year_hits) {
  66.                 $sorted_hits = array_merge($sorted_hits, $year_hits);
  67.             }
  68.         $hits[0] = $sorted_hits;
  69.        
  70.         break;
  71.  
  72.     case 'relevance':
  73.         //do nothing
  74.         break;
  75.  
  76.      }
  77.     return $hits;
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top