Created
July 25, 2019 12:43
-
-
Save jchristopher/77968ecfe147e7fdab86b41fa537b0f2 to your computer and use it in GitHub Desktop.
Give bonus weight to WooCommerce total sales in SearchWP
This file contains 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( 'searchwp_weight_mods', function( $sql ) { | |
// Multiply total number of sales by 10 to add bonus weight. | |
$multiplier = 10; | |
return $sql .= " + IF(swpwcsalesboost.meta_value+0 > 0, (swpwcsalesboost.meta_value+0) * {$multiplier}, 0)"; | |
} ); | |
add_filter( 'searchwp_query_join', function( $sql, $engine ) { | |
global $wpdb; | |
return $sql . " LEFT JOIN {$wpdb->postmeta} AS swpwcsalesboost ON {$wpdb->posts}.ID = swpwcsalesboost.post_id AND swpwcsalesboost.meta_key = 'total_sales'"; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment