Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active March 9, 2017 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/179e51bef82af7fcc77cc90322d09801 to your computer and use it in GitHub Desktop.
Save jchristopher/179e51bef82af7fcc77cc90322d09801 to your computer and use it in GitHub Desktop.
<?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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment