\SWP_Query
\SWP_Query は、\SearchWP\Query の代替となるもので、いくつか重要な違いがあります。
- 設計および実装は
WP_Queryをモデルにしています。 - その結果、
\SearchWP\Engineの設定に関係なく、結果はWP_Postに基づく\SearchWP\Source(例:投稿、固定ページ、メディア、カスタム投稿タイプ)に限定されます。
基本的な使い方
\SWP_Query は、WP_Query と同じように(場合によっては置き換えて)使用できます。
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 | |
| //@link https://searchwp.com/documentation/classes/swp_query/ | |
| global $post; | |
| $swp_query = new SWP_Query( [ | |
| 's' => 'coffee', // Search query. | |
| 'engine' => 'default', // Engine name. | |
| ] ); | |
| if ( $swp_query->have_posts() ) { | |
| while ( $swp_query->have_posts() ) : | |
| $swp_query->the_post(); | |
| ?> | |
| <div class="search-result"> | |
| <h3><a href="<?php echo get_permalink(); ?>"> | |
| <?php the_title(); ?> | |
| </a></h3> | |
| <?php the_excerpt(); ?> | |
| </div> | |
| <?php | |
| endwhile; | |
| wp_reset_postdata(); | |
| } else { | |
| ?> | |
| <p>No results found.</p> | |
| <?php | |
| } |
注意: \SWP_Query は WP_Query と機能が完全に一致するわけではないという注意点があります。利用可能なメソッドとプロパティの概要については、このドキュメントを参照してください。
また注意: \SWP_Query は(主に)WP_Query と相互運用できるように設計されているため、(例:s(検索クエリ)パラメータがない場合など)WP_Query に戻して結果を取得できる場合があります。その方法は以下のとおりです。
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 | |
| // @link https://searchwp.com/documentation/classes/swp_query/ | |
| global $post; | |
| $args = [ | |
| 's' => get_search_query(), | |
| 'tax_query' => [ [ | |
| 'taxonomy' => 'people', | |
| 'field' => 'slug', | |
| 'terms' => 'bob', | |
| ] ], | |
| ]; | |
| // If a search query is present use SWP_Query | |
| // else fall back to WP_Query | |
| if ( ! empty( $args['s'] ) ) { | |
| $swp_query = new SWP_Query( $args ); | |
| } else { | |
| $swp_query = new WP_Query( $args ); | |
| } | |
| // Loop through results. | |
| if ( $swp_query->have_posts() ) { | |
| while ( $swp_query->have_posts() ) : | |
| $swp_query->the_post(); | |
| ?> | |
| <div class="search-result"> | |
| <h3><a href="<?php echo get_permalink(); ?>"> | |
| <?php the_title(); ?> | |
| </a></h3> | |
| <?php the_excerpt(); ?> | |
| </div> | |
| <?php | |
| endwhile; | |
| wp_reset_postdata(); | |
| } else { | |
| ?> | |
| <p>No results found.</p> | |
| <?php | |
| } |
引数
新しい \SearchWP\Attribute をインスタンス化する際に、以下のいずれか array を提供できます:
s(string)- 検索クエリ。(デフォルト:
'') engine(string)\SearchWP\Engineの名前。(デフォルト:'default')posts_per_page(integer)- 1ページあたりの投稿数。(デフォルト:
get_option( 'posts_per_page' )) load_posts(boolean)- 結果を
WP_Postとして返すかどうか。(デフォルト:true) fields(string)"all"または"ids"を返すかどうか。(デフォルト:"all")nopaging(boolean)- ページネーションを無効にしてすべての結果を返すかどうか。(デフォルト:
false) page(integer)- 返す結果のページ。(デフォルト:
null) paged(integer)- 返す結果のページ。(デフォルト:
1) post__in(integer[])- 検索対象をこれらのIDに限定します。(デフォルト:
[]) post__not_in(integer[])- 検索対象をこれらのIDに限定します。(デフォルト:
[]) post_type(string[])- 結果をこれらの投稿タイプに限定します。(デフォルト:
[]) post_status(string[])- 結果をこれらの投稿ステータスに限定します。(デフォルト:
[]) tax_query([])- 参照
tax_query(デフォルト:[]) meta_query([])- 参照
meta_query(デフォルト:[]) date_query([])- 参照
date_query(デフォルト:[]) order(string)"ASC"または"DESC"の順序で結果を返します。(デフォルト:"DESC")orderby(string)"relevance"、"date"、または"rand"でソートされた結果を返します。(デフォルト:"relevance")
メソッド
注意: プロパティにはマジックゲッターとセッターが利用可能です。
get_search_results()- 現在のプロパティに基づいて結果セットを更新します。
get_posts()- 現在の結果セットを返します。
フック
\SWP_Query の動作をさらに変更するために、いくつかのフックが利用可能です:

