Skip to content

Instantly share code, notes, and snippets.

@msaari
Created March 7, 2017 04:08
Show Gist options
  • Save msaari/45881fa538ef0087a01996aabfbe88a4 to your computer and use it in GitHub Desktop.
Save msaari/45881fa538ef0087a01996aabfbe88a4 to your computer and use it in GitHub Desktop.
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