投稿タイプ(例:投稿、固定ページ)に結果を制限
デフォルトでは、SearchWPは検索を実行するために使用されたエンジンの設定に従って結果を返します。エンジンに追加された任意の数のソースに結果を制限できるフォームを構築したい場合があります。フォームが構築されたら、エンジンによって返される結果をフォームで指定されたものに制限するためにModを使用できます。
基本的な実装は次のようになります。
すべてのフックはカスタムのSearchWP カスタマイズプラグインに追加する必要があります。
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 | |
| // Limit SearchWP results to Posts and Pages. | |
| add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
| $mod = new \SearchWP\Mod(); | |
| $mod->set_where( [ [ | |
| 'column' => 'source', | |
| 'value' => [ | |
| \SearchWP\Utils::get_post_type_source_name( 'post' ), | |
| \SearchWP\Utils::get_post_type_source_name( 'page' ) | |
| ], | |
| 'compare' => 'IN', | |
| ] ] ); | |
| $mods[] = $mod; | |
| return $mods; | |
| }, 10, 2 ); |
注意:このスニペットは、フォームの入力を考慮せずに、投稿と固定ページの結果を制限する例を示しています。
このModをフォーム入力と統合できます($_GET変数の名前がsourcesであると仮定)。
すべてのフックはカスタムのSearchWP カスタマイズプラグインに追加する必要があります。
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 | |
| // Limit SearchWP results to form field value. | |
| // `sources` is a GET array of post type names. | |
| add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
| if ( empty( $_GET['sources'] ) ) { | |
| return $mods; | |
| } | |
| $mod = new \SearchWP\Mod(); | |
| $mod->set_where( [ [ | |
| 'column' => 'source', | |
| 'value' => array_map( function( $source ) { | |
| return \SearchWP\Utils::get_post_type_source_name( $source ); | |
| }, $_GET['sources'] ), | |
| 'compare' => 'IN', | |
| ] ] ); | |
| $mods[] = $mod; | |
| return $mods; | |
| }, 10, 2 ); |
注意:このスニペットは、フォーム変数が投稿タイプ名の配列を構築するチェックボックスとして設定されており、投稿タイプ名が使用中のエンジンに追加されたものと一致することを前提としています。例:
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
| <form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>"> | |
| <label> | |
| <span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span> | |
| <input type="search" class="search-field" | |
| placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" | |
| value="<?php echo get_search_query() ?>" name="s" | |
| title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" /> | |
| </label> | |
| <p> | |
| <input type="checkbox" class="search-field" id="source-post" | |
| value="post" name="sources[]" /> | |
| <label for="source-post">Posts</label> | |
| </p> | |
| <p> | |
| <input type="checkbox" class="search-field" id="source-page" | |
| value="post" name="sources[]" /> | |
| <label for="source-page">Pages</label> | |
| </p> | |
| <input type="submit" class="search-submit" | |
| value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" /> | |
| </form> |
(特に9〜18行目)

