Basic extensions for searching in WordPress.
Quick links: Install | Usage | Dimensions | Example
Note: This basic extension is not very scalable and meant for smaller databases. This package could get slow for complex searches. In that case, Elasticsearch would be a better solution.
composer require trendwerk/search
$search = new \Trendwerk\Search\Search();
$search->init();
This code should be run when bootstrapping your theme.
Currently this package supports metadata and terms as search dimensions. Dimensions can be added by using addDimension
:
$search->addDimension($dimension);
Parameter | Default | Required | Description |
---|---|---|---|
$dimension |
null |
Yes | Should be an instance of a class that implements Dimension\Dimension . |
$metaDimension = new \Trendwerk\Search\Dimension\Meta($wpdb, [
'key' => 'firstName',
]);
$search->addDimension($metaDimension);
Available options for constructing an instance of Meta
:
Parameter | Default | Required | Description |
---|---|---|---|
key |
null |
Yes | The meta_key to search for |
compare |
= |
No | The database comparison that should be made for the meta key. Currently supports LIKE and = . When using LIKE , make sure to include a percent symbol (% ) in your key parameter as a wildcard. See Example |
$search->addDimension(new \Trendwerk\Search\Dimension\Term($wpdb, [
'taxonomy' => 'taxonomyName',
]));
Available options for constructing an instance of Term
:
Parameter | Default | Required | Description |
---|---|---|---|
taxonomy |
null |
Yes | The taxonomy which terms should be included in search |
use Trendwerk\Search\Dimension\Meta;
use Trendwerk\Search\Dimension\Term;
use Trendwerk\Search\Search;
$search = new Search();
$search->init();
$search->addDimension(new Meta($wpdb, [
'compare' => 'LIKE',
'key' => 'lastNames%',
]));
$search->addDimension(new Meta($wpdb, [
'key' => 'firstName',
]));
$search->addDimension(new Term($wpdb, [
'taxonomy' => 'category',
]));