Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add search weight to more recently published entries that decays over time in SearchWP
<?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->weight( 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
You can’t perform that action at this time.