Skip to content

Instantly share code, notes, and snippets.

@msaari
Last active October 26, 2018 03:05
Show Gist options
  • Select an option

  • Save msaari/61b1e8ecc6fd3e3a667d4183410706f5 to your computer and use it in GitHub Desktop.

Select an option

Save msaari/61b1e8ecc6fd3e3a667d4183410706f5 to your computer and use it in GitHub Desktop.
Relevanssi category sorting
<?php
// Add this to theme functions.php
add_filter( 'relevanssi_hits_filter', 'rlv_category_sort' );
function rlv_category_sort( $hits ) {
$gold = array();
$silver = array();
$bronze = array();
$everything_else = array();
foreach ( $hits[0] as $hit ) {
$sorted = false;
foreach ( get_the_category( $hit->ID ) as $cat ) {
if ( 'gold' === strtolower( $cat->name ) ) {
array_push( $gold, $hit );
$sorted = true;
break;
}
if ( 'silver' === strtolower( $cat->name ) ) {
array_push( $silver, $hit );
$sorted = true;
break;
}
if ( 'bronze' === strtolower( $cat->name ) ) {
array_push( $bronze, $hit );
$sorted = true;
break;
}
}
if ( ! $sorted ) {
array_push( $everything_else, $hit );
}
}
$hits[0] = array_merge( $gold, $silver, $bronze, $everything_else );
return $hits;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment