Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Created February 8, 2022 13:49
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 searchwpgists/015e331b08a0e2c320495bd9ff0050f2 to your computer and use it in GitHub Desktop.
Save searchwpgists/015e331b08a0e2c320495bd9ff0050f2 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
<?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