Created
October 10, 2018 06:44
-
-
Save msaari/c5f4eeba6aeecc8fc33705e434d7aa8b to your computer and use it in GitHub Desktop.
Relevanssi user profile search with custom field weighting
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 | |
| // First, make sure user profiles are indexed with the meta fields. | |
| // Here's how you run the search. Get the search terms from wherever and place them in $search_terms. | |
| $args = array( | |
| 's' => $search_terms, | |
| 'post_types' => 'user', | |
| 'posts_per_page' => 10, | |
| ); | |
| $query = new WP_Query(); | |
| $query->parse_query( $args ); | |
| relevanssi_do_query( $query ); | |
| // Now the results are in $query->posts. | |
| // In order to do the custom field weighting, you need this function: | |
| add_filter( 'relevanssi_match', 'rlv_user_custom_field' ); | |
| function rlv_user_custom_field( $match ) { | |
| if ( 'u_' === substr( $match->doc, 0, 2 ) ) { | |
| // This is a user profile. | |
| $customfield_detail = json_decode( $match->customfield_detail ); | |
| if ( isset( $customfield_detail['color'] ) ) { | |
| // There's a match for color. Adjust the weight as necessary. | |
| $match->weight = $match->weight * 1; | |
| } | |
| if ( isset( $customfield_detail['material'] ) ) { | |
| // There's a match for material. Adjust the weight as necessary. | |
| $match->weight = $match->weight * 2; | |
| } | |
| } | |
| return $match; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment