Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('relevanssi_hits_filter', 'order_the_results');
- function order_the_results($hits) {
- global $wp_query;
- switch ($wp_query->query_vars['orderby']) {
- case 'evaluation': //name of my meta_key
- $eval = array();
- foreach ($hits[0] as $hit) {
- $evalcount = get_post_meta($hit->ID, '_evaluation', true);
- if (!isset($eval[$evalcount])) $eval[$evalcount] = array();
- array_push($eval[$evalcount], $hit);
- }
- if ($wp_query->query_vars['order'] == 'desc') {
- ksort($eval);
- } else {
- krsort($eval);
- }
- $sorted_hits = array();
- foreach ($eval as $evalcount => $year_hits) {
- $sorted_hits = array_merge($sorted_hits, $year_hits);
- }
- $hits[0] = $sorted_hits;
- break;
- case 'rate':
- $rate = array();
- foreach ($hits[0] as $hit) {
- $ratecount = get_post_meta($hit->ID, '_rate', true);
- if (!isset($rate[$ratecount])) $rate[$ratecount] = array();
- array_push($rate[$ratecount], $hit);
- }
- if ($wp_query->query_vars['order'] == 'desc') {
- ksort($rate);
- } else {
- krsort($rate);
- }
- $sorted_hits = array();
- foreach ($rate as $ratecount => $year_hits) {
- $sorted_hits = array_merge($sorted_hits, $year_hits);
- }
- $hits[0] = $sorted_hits;
- break;
- case 'price':
- $price = array();
- foreach ($hits[0] as $hit) {
- $pricecount = get_post_meta($hit->ID, '_price', true);
- if (!isset($price[$pricecount])) $price[$pricecount] = array();
- array_push($price[$pricecount], $hit);
- }
- if ($wp_query->query_vars['order'] == 'asc') {
- ksort($price);
- } else {
- krsort($price);
- }
- $sorted_hits = array();
- foreach ($price as $pricecount => $year_hits) {
- $sorted_hits = array_merge($sorted_hits, $year_hits);
- }
- $hits[0] = $sorted_hits;
- break;
- case 'relevance':
- //do nothing
- break;
- }
- return $hits;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement