Created
March 3, 2021 19:32
-
-
Save jchristopher/7618b076f86dd14d1d386d1844c1a7f2 to your computer and use it in GitHub Desktop.
Add secondary sort to SearchWP results to sort matching relevance results by Title in ASC order
This file contains hidden or 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 | |
// Add secondary sort to SearchWP results to sort matching | |
// relevance results by Title in ASC order. | |
add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
global $wpdb; | |
$mod = new \SearchWP\Mod(); | |
$mod->set_local_table( $wpdb->posts ); | |
$mod->on( 'ID', [ 'column' => 'id' ] ); | |
$mod->order_by( function( $mod ) { | |
return $mod->get_local_table_alias() . '.post_title'; | |
}, 'ASC', 99 ); | |
$mods[] = $mod; | |
return $mods; | |
}, 30, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment