Created
March 7, 2017 04:08
-
-
Save msaari/45881fa538ef0087a01996aabfbe88a4 to your computer and use it in GitHub Desktop.
Relevanssi filter to get exact title matches on top
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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