Documentation

View the installation guide, browse the Knowledge Base, find out about SearchWP's many hooks, get support

Add Weight to More Recently Published Entries

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_weight_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 theme’s functions.php:

<?php
// Adjust calculated weight to consider how recently the entry was published.
add_filter( 'searchwp_weight_mods', function( $sql ) {
global $wpdb;
// Depending on the resulting calculation, you can modify how much the date influences the total weight.
$modifier = 1;
$sql .= " + ( ( UNIX_TIMESTAMP( NOW() ) - ( UNIX_TIMESTAMP( NOW() ) - UNIX_TIMESTAMP( {$wpdb->posts}.post_date ) ) - (SELECT UNIX_TIMESTAMP( post_date ) FROM {$wpdb->posts} WHERE post_status = 'publish' ORDER BY post_date ASC LIMIT 1 ) ) / 86400 ) * {$modifier}";
return $sql;
} );
view raw functions.php hosted with ❤ by GitHub

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!

<?php
/**
* Add search weight to more recently published entries
*/
function my_searchwp_add_weight_to_newer_posts( $sql ) {
global $wpdb;
// Adjust how much weight is given to newer publish dates. There
// is no science here as it correlates with the other weights
// in your engine configuration, and the content of your site.
// Experiment until results are returned as you want them to be.
$modifier = 15;
$sql .= " + ( 100 * EXP( ( 1 - ABS( ( UNIX_TIMESTAMP( {$wpdb->prefix}posts.post_date ) - UNIX_TIMESTAMP( NOW() ) ) / 86400 ) ) / 1 ) * {$modifier} )";
return $sql;
}
add_filter( 'searchwp_weight_mods', 'my_searchwp_add_weight_to_newer_posts' );
view raw functions.php hosted with ❤ by GitHub

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 13 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:

<?php
/**
* Tell SearchWP to give extra weight to results based on a date stored as a Custom Field
* value. The more recent the date the more bonus weight is given. The Custom Field value
* in the database needs to be UNIX_TIMESTAMP()-compatible (e.g. YYYYMMDD) which Advanced
* Custom Fields does by default.
*
* Customize $date_meta_key to be that of your meta_key and adjust the modifier to your liking.
*/
class MySearchwpMetaDateWeightMods {
private $date_meta_key = 'display_date'; // Needs to store data as YYYYMMDD (ACF does this already).
private $modifier = 1;
function __construct() {
add_filter( 'searchwp_query_join', array( $this, 'searchwp_query_join' ), 10, 2 );
add_filter( 'searchwp_weight_mods', array( $this, 'searchwp_weight_mods' ) );
}
function searchwp_query_join( $sql, $engine ) {
global $wpdb;
return $sql . " LEFT JOIN {$wpdb->postmeta} AS searchwpmetadatesort ON {$wpdb->posts}.ID = searchwpmetadatesort.post_id AND searchwpmetadatesort.meta_key = '{$this->date_meta_key}'";
}
function searchwp_weight_mods( $sql ) {
global $wpdb;
return $sql . " + ( ( UNIX_TIMESTAMP( NOW() ) - ( UNIX_TIMESTAMP( NOW() ) - UNIX_TIMESTAMP( searchwpmetadatesort.meta_value ) ) - (SELECT UNIX_TIMESTAMP( meta_value ) FROM {$wpdb->postmeta} WHERE meta_key = '{$this->date_meta_key}' ORDER BY meta_value ASC LIMIT 1 ) ) / 86400 ) * {$this->modifier}";
}
}
new MySearchwpMetaDateWeightMods();
view raw functions.php hosted with ❤ by GitHub
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