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

\SearchWP\Source

\SearchWP\Source is an abstract class designed to model content types for SearchWP’s indexing and search processes. \SearchWP\Sources are added to Engines which are in turn used when instantiating a \SearchWP\Query.

In SearchWP there are a number of core \SearchWP\Sources available by default, including:

Because these \SearchWP\Sources are available by default, they can be used immediately when configuring your Engines.

Basic Usage

You can build your own \SearchWP\Sources to match any custom content types you have (including those with custom database tables) by using this class and the searchwp\sources hook.

<?php
/**
* Adds custom SearchWP\Source for MyCustomSources.
*/
add_action( 'plugins_loaded', function() {
class MyCustomSource extends \SearchWP\Source {
// Unique name for this source.
protected $name = 'mycustomsource';
// Database column name that tracks entries for this Source.
protected $db_id_column = 'id';
function __construct() {
global $wpdb;
// The database table storing entries.
$this->db_table = $wpdb->get_blog_prefix() . 'my_db_table';
// Labels for this Source.
$this->labels = [
'plural' => 'My Custom Source Entries',
'singular' => 'My Custom Source Entry',
];
// Attributes for this Source.
$this->attributes = [ [
'name' => 'content',
'label' => 'Entry Content',
'default' => \SearchWP\Utils::get_min_engine_weight(),
'data' => function( $entry_id ) {
return MyCustomSource::get_content( $entry_id );
},
], ];
}
// Returns a native object.
public function entry( \SearchWP\Entry $entry, $query = false ) {
return MyCustomSource::get_entry( $entry->get_id() );
}
}
// Append this Source to the list of available Sources in SearchWP.
add_filter( 'searchwp\sources', function( $sources ) {
$sources[] = new MyCustomSource();
return $sources;
} );
} );

Note: Any registered \SearchWP\Source can also be removed by using the searchwp\sources hook as well.

Arguments

There are no arguments when instantiating a new \SearchWP\Source.

Properties

When extending \SearchWP\Source there are a number of properties to consider.

name (string)
A unique name. (default: '')
labels (array)
Labels, array with singular and plural keys. (default: [])
db_table (string)
The database table used to store entries to be indexed. (default: '')
db_id_column (string)
The database column used to track entry IDs. (default: '')
attributes (array)
Individual Attributes to receive a relevance weight when configuring this Source in an Engine. (default: [])
Array of Attributes or Attribute settings arrays, each with the following keys:
'name' (string) Attribute name.
'label' (string) Attribute label.
'default' (integer) Default weight (zero to omit as default).
'options' (array) Optional. Defines individual instances of this Attribute.
'data' (mixed) Defines the data for this Attribute when instantiating an Entry.
– 1st parameter is the entry ID as per $db_id_column
– 2nd parameter (when applicable) is the chosen option from options
rules (array)
Individual Rules to control the candidacy of Entries as search results. (default: [])
Array of Rules or Rule settings arrays, each with the following keys:
'name' (string) Rule name.
'label' (string) Rule label.
'options' (array) Optional. Defines individual options for this Rule.
'conditions' (array) Optional. Defines individual conditions of this Rule.
'values' (array) Optional. Defines values for each option of this Rule.
'application' (mixed) Applies the rule logic.
– 1st parameter contains the properties of the rule as an array
option The chosen option
condition The chosen condition
value The chosen value

Methods

When extending \SearchWP\Source there are a number of methods to consider.

db_where (return array)
Array of WHERE clause arrays that must be met for both indexing and searching. (default: [])
'column' (string) Database column
'value' (mixed) Value
'type' (string) Type of value(s), can be 'CHAR' or 'NUMERIC'
'compare' (string) Method by which to compare value
Available comparison types include: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS', 'NOT EXISTS'
entry (return mixed)
When a search result is found, this method is fired to retrieve a native representation of that result. (default: stdClass)
- 1st parameter is the \SearchWP\Entry that was found
- 2nd parameter is the \SearchWP\Query that found this result (if applicable)
add_hooks
Fired upon initialization of this Source. Primarily used to implement hooks to facilitate \SearchWP\Index\Controller updates as content is edited.

Hooks

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

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