\SearchWP\Query
Table of Contents
\SearchWP\Query is the class responsible for executing searches by utilizing an existing Engine.
Basic Usage
All usage of \SearchWP\Query requires at least one parameter; a search query:
| <?php | |
| // @link https://searchwp.com/documentation/classes/searchwp-query/ | |
| $search = new \SearchWP\Query( 'coffee' ); | |
| $results = $search->results; // Array of results. |
By default $search->results will be an array of objects, formatted like so:
stdClass Object
(
[id] => 1 // Result ID.
=> post.post // Result Source name.
[site] => 1 // Result site ID.
[relevance] => 150 // Result relevance weight.
)
This generic stdClass result provides the bare essentials of what is needed to retrieve additional information about the result.
Arguments
An array of arguments can be passed as the second parameter when instantiating \SearchWP\Query. There are a number of arguments available to customize the results returned.
| <?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. |
Here is the full list of available arguments:
engine(string)- The Engine name to use for the search. (default:
'default') mods(\SearchWP\Mod[])- Array of
\SearchWP\Mods to use for the search. (default:[]) site(integer[])- Array of site IDs to use for the search. (default:
get_current_blog_id()) per_page(integer)- How many results to return per page. (default:
get_option( 'posts_per_page' )) page(integer)- Which page of results to return. (default:
1) offset(integer)- Offset of results to return. (default:
0) fields(string)- Fields to return for each result. (default:
'default') - Accepts:
'default','ids','all', or'entries' 'default'Returnsobject[]with properties:'id','source','site','relevance'(weight).'ids'Returnsstring[]of result IDs (Note: Source is not supplied, use only when Source can be inferred).'all'Returns array of native Source objects.'entries'Returns\SearchWP\Entry[]of results.
Methods
get_results()- Returns the results.
get_raw_results()- Returns the default set of results, without the application of the
fieldsargument. get_sql()- Returns the SQL query used to find results.
get_args()- Returns the arguments used for the search.
get_tokens()- Returns the
\SearchWP\Tokensused in the query. get_keywords()- Returns the original, submitted search string.
get_errors()- Returns any generated Errors. Useful when debugging.
get_suggested_search()- Returns the suggested search if applicable.
get_engine()- Returns the
\SearchWP\Enginein use. run()- Re-runs the search query using current properties.
Hooks
There are a number of hooks available to further modify the behavior of \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

