Last active
April 14, 2021 15:36
-
-
Save jchristopher/83f38d2079bbc34f32dcfb6f9c0f2f3a to your computer and use it in GitHub Desktop.
Add search weight to more recently published entries that decays over time 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 search weight to more recently published entries in SearchWP. | |
// Weight decays over time and eventually will not add bonus weight. | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
global $wpdb; | |
$weight_adjust = 15; | |
$mod = new \SearchWP\Mod(); | |
$mod->set_local_table( $wpdb->posts ); | |
$mod->on( 'ID', [ 'column' => 'id' ] ); | |
$mod->relevance( function( $runtime_mod ) use ( $weight_adjust ) { | |
$alias = $runtime_mod->get_local_table_alias(); | |
return " | |
( 100 * EXP( | |
( 1 - ABS( ( | |
UNIX_TIMESTAMP( {$alias}.post_date ) | |
- UNIX_TIMESTAMP( NOW() ) | |
) / 86400 ) ) / 1 ) | |
* {$weight_adjust} )"; | |
} ); | |
$mods[] = $mod; | |
return $mods; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment