Skip to content

Instantly share code, notes, and snippets.

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