trendwerk/search

Basic extensions for searching in WordPress.

0.2.3 2018-10-16 18:22 UTC

This package is auto-updated.

Last update: 2024-11-26 20:54:04 UTC


README

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);

Meta

$metaDimension = new \Trendwerk\Search\Dimension\Meta($wpdb, [
	'key' => 'firstName',
]);

$search->addDimension($metaDimension);

Available options for constructing an instance of Meta:

Terms

$search->addDimension(new \Trendwerk\Search\Dimension\Term($wpdb, [
	'taxonomy' => 'taxonomyName',
]));

Available options for constructing an instance of Term:

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