Created
September 13, 2018 03:46
-
-
Save msaari/835435ae623a501c2d033e4ea2f0556e to your computer and use it in GitHub Desktop.
Add boost to exact matches in custom fields
This file contains hidden or 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_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