SearchWP

\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
// @link https://searchwp.com/documentation/classes/searchwp-source/
/**
* Adds custom SearchWP\Source for MyCustomSearchWPSource which uses a custom database table.
*/
add_action( 'plugins_loaded', function() {
class MyCustomSearchWPSource extends \SearchWP\Source {
// Unique name for this Source.
protected $name = 'mycustomsource';
// Database column name that provides a unique ID for each entry of the 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. Array of attributes, can be expanded.
$this->attributes = [ [
'name' => 'content',
'label' => 'Entry Content',
'default' => \SearchWP\Utils::get_min_engine_weight(),
'data' => function( $entry_id ) {
// Note: MyWidget is an pseudo-class meant to represent
// a class you're using that represents something like
// WP_Post in that it models the data you are working with.
// Long story short: this method should return the content
// you want to index and make searchable in SearchWP.
return MyWidget::get_content( $entry_id );
},
], ];
}
// Returns a native object e.g. formatted in a way you expect.
public function entry( \SearchWP\Entry $entry, $query = false ) {
// Note: MyWidget is a pseudo-class meant to represent a class
// you're using that represents something like WP_Post in that it
// models the data you're working with.
// Long story short: this method should return whatever it is you
// expect when working 'natively' with your data e.g. for WordPress
// pages a WP_Post is returned. Note that $entry->get_id() returns
// the value in the $db_id_column for this Source.
return new MyWidget( $entry->get_id() );
}
}
// Append this Source to the list of available Sources in SearchWP.
add_filter( 'searchwp\sources', function( $sources ) {
$sources[] = new MyCustomSearchWPSource();
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: