-
-
Save searchwpgists/928851c351034383509f35ac02df41cd to your computer and use it in GitHub Desktop.
Add search weight to more recently published entries 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. | |
// @link https://searchwp.com/documentation/knowledge-base/add-relevance-weight-date/ | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
global $wpdb; | |
$mod = new \SearchWP\Mod(); | |
$mod->set_local_table( $wpdb->posts ); | |
$mod->on( 'ID', [ 'column' => 'id' ] ); | |
$mod->relevance( function( $runtime ) use ( $wpdb ) { | |
return " | |
COALESCE( ROUND( ( ( | |
UNIX_TIMESTAMP( {$runtime->get_local_table_alias()}.post_date ) | |
- ( | |
SELECT UNIX_TIMESTAMP( {$wpdb->posts}.post_date ) | |
FROM {$wpdb->posts} | |
WHERE {$wpdb->posts}.post_status = 'publish' | |
ORDER BY {$wpdb->posts}.post_date ASC | |
LIMIT 1 | |
) | |
) / 86400 ), 0 ), 0 )"; | |
} ); | |
$mods[] = $mod; | |
return $mods; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment