Advertisement
parkerbennett

relevanssi_hits_filter alphabetize field

Dec 7th, 2014
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////
  2. // Alphabetize Search Results from Relevanssi
  3. // Group Projects, Posts
  4. /////////////////////////////////////////////////////////////
  5.  
  6. add_filter('relevanssi_hits_filter', 'order_the_results');
  7. function order_the_results($hits) {
  8.  
  9. global $wp_query;
  10.  
  11. $alphabetize = array();
  12.  
  13. if (!empty($hits)) {
  14.  
  15. foreach ($hits[0] as $hit) {
  16. $alphavalue = get_post_meta($hit->ID, 'alphabetize', true);
  17. if (!isset($alphabetize[$alphavalue])) $alphabetize[$alphavalue] = array();
  18. array_push($alphabetize[$alphavalue], $hit);
  19. }
  20.  
  21. // sortby ascending
  22. ksort($alphabetize);
  23.  
  24. $sorted_hits = array();
  25.  
  26. foreach ($alphabetize as $alphavalue => $alpha_hits) {
  27. $sorted_hits = array_merge($sorted_hits, $alpha_hits);
  28. }
  29.  
  30. $hits[0] = $sorted_hits;
  31.  
  32. // split post_types to show projects first
  33. $types['project'] = array();
  34. $types['post'] = array();
  35.  
  36. foreach ($hits[0] as $hit) {
  37. if (!is_array($types[$hit->post_type])) $types[$hit->post_type] = array();
  38. array_push($types[$hit->post_type], $hit);
  39. }
  40.  
  41. // Merge back to $hits in the desired order
  42. $hits[0] = array_merge($types['project'], $types['post']);
  43. }
  44. return $hits;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement