Prioritizing search results
-
I am trying to prioritize certain search results on my site. I have a custom field called “priorityplace” controlled by a checkbox. What I want to happen is for search results to place results with this item checked at the top of the results with the ones without it following.
I tried using some of the code on this page: https://www.relevanssi.com/user-manual/relevanssi_hits_filter/
Here’s my version of the code below which is placed at the bottom of the functions.php file. I’m guessing I did something wrong but I don’t know what. (I’m not very good with PHP yet)
Thanks in advance.
—–
add_filter(‘relevanssi_hits_filter’, ‘order_the_results’);
function order_the_results($hits) {
global $wp_query;switch ($wp_query->query_vars[‘orderby’]) {
case ‘likes’:
$likes = array();
foreach ($hits[0] as $hit) {
$likecount = get_post_meta($hit->ID, ‘priorityplace’, true);
if (!isset($likes[$likecount])) $likes[$likecount] = array();
array_push($likes[$likecount], $hit);
}if ($wp_query->query_vars[‘order’] == ‘asc’) {
ksort($likes);
} else {
krsort($likes);
}$sorted_hits = array();
foreach ($likes as $likecount => $year_hits) {
$sorted_hits = array_merge($sorted_hits, $year_hits);
}
$hits[0] = $sorted_hits;
break;case ‘relevance’:
//do nothing
break;}
return $hits;
}
—The page I need help with: [log in to see the link]
- The topic ‘Prioritizing search results’ is closed to new replies.