\SearchWP\Query
\SearchWP\Query は、既存のEngineを利用して検索を実行するクラスです。
基本的な使い方
\SearchWP\Query のすべての使用には、少なくとも1つのパラメータ、検索クエリが必要です。
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/searchwp-query/ | |
| $search = new \SearchWP\Query( 'coffee' ); | |
| $results = $search->results; // Array of results. |
デフォルトでは、$search->results は次のようにフォーマットされたオブジェクトの配列になります。
stdClass Object
(
[id] => 1 // Result ID.
=> post.post // Result Source name.
[site] => 1 // Result site ID.
[relevance] => 150 // Result relevance weight.
)
この汎用的なstdClassの結果は、結果に関する追加情報を取得するために必要な最小限の要素を提供します。
引数
\SearchWP\Query をインスタン化する際に、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
| <?php | |
| // @link https://searchwp.com/documentation/classes/searchwp-query/ | |
| $search = new \SearchWP\Query( 'coffee', [ | |
| 'engine' => 'supplemental', | |
| 'per_page' => -1, | |
| 'fields' => 'all', | |
| ] ); | |
| $results = $search->results; // Array of results. |
利用可能な引数の全リストはこちらです。
engine(string)- 検索に使用するEngineの名前。(デフォルト:
'default') mods(\SearchWP\Mod[])- 検索に使用する
\SearchWP\Modsの配列。(デフォルト:[]) site(integer[])- 検索に使用するサイトIDの配列。(デフォルト:
get_current_blog_id()) per_page(integer)- 1ページあたりに返す結果の数。(デフォルト:
get_option( 'posts_per_page' )) page(integer)- 返す結果のページ。(デフォルト:
1) offset(integer)- 返す結果のオフセット。(デフォルト:
0) fields(string)- 各結果に対して返すフィールド。(デフォルト:
'default') - 受け入れられる値:
'default'、'ids'、'all'、または'entries' 'default'は、'id'、'source'、'site'、'relevance'(重み)のプロパティを持つobject[]を返します。'ids'は結果IDのstring[]を返します(注意:Sourceは提供されません。Sourceが推測できる場合にのみ使用してください)。'all'はネイティブなSourceオブジェクトの配列を返します。'entries'は結果の\SearchWP\Entry[]を返します。
メソッド
get_results()- 結果を返します。
get_raw_results()fields引数を適用せずに、デフォルトの結果セットを返します。get_sql()- 結果を見つけるために使用されたSQLクエリを返します。
get_args()- 検索に使用された引数を返します。
get_tokens()- クエリで使用される
\SearchWP\Tokensを返します。 get_keywords()- 元の、送信された検索文字列を返します。
get_errors()- 生成されたエラーを返します。デバッグに便利です。
get_suggested_search()- 該当する場合、提案された検索を返します。
get_engine()- 使用中の
\SearchWP\Engineを返します。 run()- 現在のプロパティを使用して検索クエリを再実行します。
フック
\SearchWP\Queryの動作をさらに変更するために、いくつかのフックが利用可能です。
-
searchwp\query\logic\and\token_threshold -
searchwp\query\before -
searchwp\query\tokens\use_stems -
searchwp\query\tokens\limit -
searchwp\query\search_string -
searchwp\query -
searchwp\query\logic\phrase -
searchwp\query\logic\and -
searchwp\query\results -
searchwp\query\result\load_data\all_attributes -
searchwp\query\result\load_data -
searchwp\query\mods -
searchwp\query\do_source_db_where -
searchwp\query\per_page -
searchwp\query\args -
searchwp\query\output_suggested_search -
searchwp\query\logic\{$type}\strict -
searchwp\query\partial_matches\buoy -
searchwp\query\partial_matches\wildcard_after -
searchwp\query\partial_matches\wildcard_before -
searchwp\query\partial_matches\minimum_length -
searchwp\query\partial_matches\tokens -
searchwp\query\partial_matches\did_you_mean -
searchwp\query\partial_matches\force -
searchwp\query\partial_matches -
searchwp\query\partial_matches\fuzzy\minimum_length -
searchwp\query\partial_matches\fuzzy\threshold -
searchwp\query\partial_matches\fuzzy -
searchwp\query\partial_matches\fuzzy\force

