/search

Basic extensions for searching in WordPress.

Primary LanguagePHPGNU General Public License v3.0GPL-3.0

Search

Build Status codecov

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.

Install

composer require trendwerk/search

Usage

  1. Initialize package
  2. Add search dimension(s)

Initialize

$search = new \Trendwerk\Search\Search();
$search->init();

This code should be run when bootstrapping your theme.

Dimensions

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.

Meta

$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

Terms

$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

Example

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',
]));