Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use a Custom Field as a buoy to supercede SearchWP's relevance weight sorting
<?php
// Use a Custom Field as a buoy to supercede SearchWP's relevance weight sorting.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
global $wpdb;
$meta_key = 'my_buoy_meta_key';
// Add the buoy to these post types:
$post_types = [ 'post', 'page', ];
foreach ( $post_types as $post_type ) {
$source = \SearchWP\Utils::get_post_type_source_name( $post_type );
$mod = new \SearchWP\Mod( $source );
$mod->set_local_table( $wpdb->postmeta );
$mod->on( 'post_id', [ 'column' => 'id' ] );
$mod->on( 'meta_key', [ 'value' => $meta_key ] );
$mod->order_by( function( $mod ) {
return $mod->get_local_table_alias() . '.meta_value+0';
}, 'DESC', 5 );
$mods[] = $mod;
}
return $mods;
}, 30, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.