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:

$search = new \SearchWP\Query( 'coffee' );
$results = $search->results; // Array of results.
view raw template.php hosted with ❤ by GitHub

By default $search->results will be an array of objects, formatted like so:

stdClass Object
    [id]        => 1          // Result ID.
    [source]    =>  // 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.


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.

$search = new \SearchWP\Query( 'coffee', [
'engine' => 'supplemental',
'per_page' => -1,
'fields' => 'all',
] );
$results = $search->results; // Array of results.
view raw template.php hosted with ❤ by GitHub

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' Returns object[] with properties: 'id', 'source', 'site', 'relevance' (weight).
'ids' Returns string[] 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.


Returns the results.
Returns the default set of results, without the application of the fields argument.
Returns the SQL query used to find results.
Returns the arguments used for the search.
Returns the \SearchWP\Tokens used in the query.
Returns the original, submitted search string.
Returns any generated Errors. Useful when debugging.
Returns the suggested search if applicable.
Returns the \SearchWP\Engine in use.
Re-runs the search query using current properties.


There are a number of hooks available to further modify the behavior of \SearchWP\Query: