Note: You are viewing the documentation for Version 4View Version 3 Docs

\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
$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]    => 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
$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.

Methods

get_results()
Returns the results.
get_raw_results()
Returns the default set of results, without the application of the fields argument.
get_sql()
Returns the SQL query used to find results.
get_args()
Returns the arguments used for the search.
get_tokens()
Returns the \SearchWP\Tokens used 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\Engine in 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:

Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP