関連性が一致する結果の並べ替えで二次的な並べ替え順序を定義する
複数の検索結果の関連性がまったく同じで、関連性が一致する結果の並べ替え順をMySQLに決定させる場合があります。
\SearchWP\Mod を使用すると、関連性が一致する結果をどのように並べ替えたいかを指定できます。
All hooks should be added to your custom SearchWP Customizations Plugin.
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 ); |
このスニペットを使用して、関連性が一致するスコアを持つ結果を ASC 順で投稿タイトルで並べ替えるための二次並べ替えメカニズムを実装するようにSearchWPに指示しています。
これは、99 の優先度で設定されているため、二次並べ替え句です。\SearchWP\Mod で order_by() 句を設定する場合、各句は ASC 順で優先度に従って出力されます。
すべてのSearchWPクエリには、優先度 10 で DESC 順で relevance で並べ替えるためのコアデフォルト順序があります。

