Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active June 4, 2021 00:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/e9ff7a909e55aece9d1c7ee749395bb1 to your computer and use it in GitHub Desktop.
Save jchristopher/e9ff7a909e55aece9d1c7ee749395bb1 to your computer and use it in GitHub Desktop.
Sort SearchWP Post, Page, and Custom Post Type Results by date in DESC order
<?php
// Sort SearchWP Post, Page, and Custom Post Type Results by date in DESC order.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
foreach ( $query->get_engine()->get_sources() as $source ) {
$flag = 'post' . SEARCHWP_SEPARATOR;
if ( 'post.' !== substr( $source->get_name(), 0, strlen( $flag ) ) ) {
continue;
}
$mod = new \SearchWP\Mod( $source );
$mod->order_by( function( $mod ) {
return $mod->get_local_table_alias() . '.post_date';
}, 'DESC', 1 );
$mods[] = $mod;
}
return $mods;
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment