Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Relevanssi filter to get exact title matches on top
<?php
add_filter('relevanssi_hits_filter', 'rlv_find_exact_title_match');
function rlv_find_exact_title_match($hits) {
$title_match = array();
$the_rest = array();
foreach ($hits[0] as $hit) {
if (mb_strtolower($hit->post_title) == $hits[1]) {
$title_match[] = $hit;
}
else {
$the_rest[] = $hit;
}
}
global $title_match_found;
if (count($title_match) > 0) {
$title_match_found = true;
!empty($the_rest) ? $hits[0] = array_merge($title_match, $the_rest) : $hits[0] = $title_match;
}
else {
$hits[0] = $the_rest;
}
return $hits;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.