複数の単語に対して完全一致を強制する
SearchWPの引用符/フレーズ検索のサポートにより、送信された複数単語の検索と完全に一致する結果が得られます。デフォルトでは、訪問者が他の検索エンジンで一般的な慣行であるように、正確な一致文字列を二重引用符で囲む必要があります。
すべての複数単語検索で完全一致ロジックを強制したい場合は、次のフックを使用できます。
注意: SearchWP設定画面の「詳細設定」タブで引用符付き検索サポートを有効にするには、チェックボックスをオンにする必要があります。
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 | |
| // Force multiple word searches to use quoted search logic if quotes are not added. | |
| // NOTE: Quoted search must be enabled (checkbox on the Advanced tab) | |
| add_filter( 'searchwp\query\search_string', function( $search_string, $query ) { | |
| // If there are already quotes, bail out. | |
| if ( false !== strpos( $search_string, '"' ) ) { | |
| return $search_string; | |
| } | |
| // If there's only one word, bail out. | |
| if ( false === strpos( $search_string, ' ') ) { | |
| return $search_string; | |
| } | |
| return '"' . $search_string . '"'; | |
| }, 30, 2 ); |
このフックを配置すると、すべての複数単語検索が二重引用符で囲まれ、元の検索が二重引用符で囲まれていたかのように結果が生成されます。

