Note: You are viewing the documentation for Version 4View Version 3 Docs

Add Relevance Weight to More Recently Published Entries by Date

By default SearchWP returns results sorted by the total calculated weight based upon the values entered in your engine configuration. There are times when you might want to dynamically influence the ranking of search results by taking into consideration the publish date of each result.

Using SearchWP’s searchwp\query\mods hook, we can do just that! There are three different approaches to cover:

Note: This implementation does NOT cause results to be sorted by date, instead more recently published entries will simply be given more weight as calculated by SearchWP. The more recently an entry was published the more weight it gets.

See also: Using a Custom Field to Prioritize Search Results

Bonus weight for recently published

To have more recently published entries get a weight boost, add this to your SearchWP Customizations plugin:

All hooks should be added to your custom SearchWP Customizations Plugin.

<?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;
} );

Bonus weight that decays over time

Alternatively, you can use something like this to have the weight influence taper off as publish dates go further back in time. Only recently published posts will get a boost, the published date for older posts will not influence the weight.

This modification is more aggressive in that the influence tapers off over time, but that may be preferential for you!

All hooks should be added to your custom SearchWP Customizations Plugin.

<?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;
} );

With either in place, entries with more recent published dates will be given more weight than those with older publish dates.

As referenced in the comment, experiment with line 8 to determine what $modifier value works best for you.

Date stored as Custom Field value

Another example would be to have results sorted by dates stored in a Custom Field. The more recent the date of the Custom Field the more weight the result will receive:

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// Add bonus weight from Custom Field value in SearchWP.
add_filter( 'searchwp\query\mods', function( $mods ) {
global $wpdb;
// Custom Field name. Needs to store data as YYYYMMDD (ACF does this already).
$my_meta_key = 'date_field';
$mod = new \SearchWP\Mod();
$mod->set_local_table( $wpdb->postmeta );
$mod->on( 'post_id', [ 'column' => 'id' ] );
$mod->on( 'meta_key', [ 'value' => $my_meta_key ] );
$mod->weight( function( $runtime ) use ( $wpdb, $my_meta_key ) { return $wpdb->prepare( "
COALESCE( ROUND( ( (
UNIX_TIMESTAMP( {$runtime->get_local_table_alias()}.meta_value )
- (
SELECT UNIX_TIMESTAMP( meta_value )
FROM {$wpdb->postmeta}
WHERE meta_key = %s
ORDER BY meta_value ASC
LIMIT 1
)
) / 86400 ), 0 ), 0 )", $my_meta_key );
} );
$mods[] = $mod;
return $mods;
} );
Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP