Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created July 25, 2019 12:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/77968ecfe147e7fdab86b41fa537b0f2 to your computer and use it in GitHub Desktop.
Save jchristopher/77968ecfe147e7fdab86b41fa537b0f2 to your computer and use it in GitHub Desktop.
Give bonus weight to WooCommerce total sales in SearchWP
<?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