Relevanssi has the option to boost the weight of results which match all words of the search query. So when searching for “word1 word2” and a post’s title is “word1 word2 word3”, that post will appear extra high in the results, rather than simply treating it as a hit with 2 matches (1 for each word). This exact match boost only supports hits in the post title and post description by default. In some cases it can be useful to also be applied to meta data, since it’s quite common for a site to save additional types of content as metadata.
Based on the original function that works on title/description, I wrote an adaptation that applies to metadata:
// Add weight to exact matches in meta values
add_filter( 'relevanssi_results', function( $results ) {
// Search term
$query = strtolower( get_search_query() );
// List of results as post-id => weight
foreach ( $results as $post_id => $weight ) {
// Boost exact meta value hits
foreach ( get_post_meta( $post_id ) as $meta_key => $meta_values ) {
foreach ( $meta_values as $meta_value ) {
if ( stristr( $meta_value, $query ) != false ) {
$results[ $post_id ] = $weight * 100;
}
}
}
}
return $results;
} );
Can I give a users with a certain role a higher weight than other users posts? I need it to still show in order of search term matches, but then if two users posts have the same amount of matches then it show the users with the role premium before the user with the role member. Is this possible? If not, is it possible to do same as above but with post types? So for example if I had post-types of: Premium-posts and basic-posts, Still going by search results match, but show the premium-posts before basic-posts when both posts have same amount of matches?
Anonymous
hi :) bross :)
Anonymous
Ciao a tutti vengo dall’italia / itawero
Anonymous
i am from Italy hello. Can you help me translate? /rardor
Your idea is brilliant
всем успеха…
Спасибо за информацию!!!!!
Good site!
Can I give a users with a certain role a higher weight than other users posts? I need it to still show in order of search term matches, but then if two users posts have the same amount of matches then it show the users with the role premium before the user with the role member. Is this possible? If not, is it possible to do same as above but with post types? So for example if I had post-types of: Premium-posts and basic-posts, Still going by search results match, but show the premium-posts before basic-posts when both posts have same amount of matches?
hi :) bross :)
Ciao a tutti vengo dall’italia / itawero
i am from Italy hello. Can you help me translate? /rardor