投稿、ページ、カスタム投稿タイプを日付で並べ替える
デフォルトでは、SearchWP は関連性の高い順に結果を並べ替えます。SearchWP Mode を使用すると、結果の並べ替えを上書きして、公開された順序(新しいものから古いものへ)で返すことができます。
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 | |
| // 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 ); |
このフックは WP_Post ベースのソース(投稿、固定ページ、カスタム投稿タイプなど)にのみ適用されることに注意してください。ただし、使用しているカスタムソースを考慮するように変更することもできます。

