Last active
April 22, 2021 20:28
-
-
Save jchristopher/7fe0117f98318e71fcdcd9de116def62 to your computer and use it in GitHub Desktop.
Give Products extraordinary weight boost to ensure Products show up first.
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 | |
// Give Products extraordinary weight boost to ensure Products show up first. | |
// @link https://searchwp.com/documentation/knowledge-base/post-type-first-top/ | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
$post_type = 'product'; // Post type name. | |
$source = \SearchWP\Utils::get_post_type_source_name( $post_type ); | |
$mod = new \SearchWP\Mod( $source ); | |
$mod->relevance( function( $runtime ) use ( $source ) { | |
global $wpdb; | |
return $wpdb->prepare( | |
"IF( {$runtime->get_foreign_alias()}.source = %s, '999999999999', '0' )", | |
$source | |
); | |
} ); | |
$mods[] = $mod; | |
return $mods; | |
} ); |
I found that sometimes $mods
is not an array, so to avoid fatal errors, I had to implement this at the start of the function:
if ( ! is_array( $mods ) ) {
$mods = array( $mods );
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, I have this working great now with the code below. However, I'm getting this notice using debug mode. Is this expected because of an unusual use case of wpdb or is there some way I can fix it?