Hi Mikko
Thank you for your reply, for anyone else who wants to put the exact match SKU results above the rest here is the code in my child theme functions file.
It is a little lazy as all I have changed is the search variable to sku for the URL and _sku for the field to check for the result in.
add_filter('relevanssi_hits_filter', 'order_the_results');
function order_the_results($hits) {
global $wp_query;
switch ($wp_query->query_vars['orderby']) {
case 'sku':
$likes = array();
foreach ($hits[0] as $hit) {
$likecount = get_post_meta($hit->ID, '_sku', 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;
}
And this is the Search form on https://www.teng.mossdemo.com.au/
<form class="search" action="<?php echo home_url(); ?>/" method="get">
<p>Enter Part Number or any other term</p>
<fieldset>
<span class="text"><input name="s" id="s" type="text" value="" placeholder="<?php echo __('Search ...', 'Avada'); ?>" /></span>
</fieldset>
<input name="orderby" type="hidden" id="orderby" value="sku" />
</form>
I don’t know whether that adding a hidden field in the search form was the best way to append the ‘&orderby=sku’ to the URL but as my theme had the searchform.php like this, it was the laziest way to do it!