Skip to content

Instantly share code, notes, and snippets.

@msaari
Created September 13, 2018 03:46
Show Gist options
  • Select an option

  • Save msaari/835435ae623a501c2d033e4ea2f0556e to your computer and use it in GitHub Desktop.

Select an option

Save msaari/835435ae623a501c2d033e4ea2f0556e to your computer and use it in GitHub Desktop.
Add boost to exact matches in custom fields
<?php
add_filter( 'relevanssi_results', 'rlv_exact_boost' );
function rlv_exact_boost( $results ) {
$query = strtolower( get_search_query() );
foreach ( $results as $post_id => $weight ) {
$custom_fields = get_post_meta( $post_id );
foreach ( $custom_fields as $field => $values ) {
if ( in_array( $field, array( 'custom_field_1', 'custom_field_2', 'custom_field_3' ), true ) ) {
$values = implode( ' ', $values );
if ( stristr( $values, $query ) !== false ) {
$results[ $post_id ] = $weight * 1000;
}
}
}
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment