-
-
Save searchwpgists/4c06f705e344a812870387b34238786b to your computer and use it in GitHub Desktop.
Sort SearchWP Post, Page, and Custom Post Type Results by date in DESC order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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