Advanced filters & excludes #

When performing a search, in addition to simple single-value and array-of-value filters, it’s possible to build more complex filter/exclude criteria. Some examples of this include:

Usage #

Advanced search/exclude filters are constructed from Criteria objects, from the MinimalCode Solr Search Criteria package. More information and usage examples are available here.

When passing a Criteria object to addFilter or addExclude, the first argument (usually the field name) can be set to any string value.

$query = new BaseQuery();

// Simple date filter - exclude any pages which have an embargo date in the future
$criteria = Criteria::where('SiteTree_Embargo')
    ->greaterThanEqual(new \DateTime('NOW'));
$query->addExclude('embargo', $criteria);

// Starts with/ends with filter
$criteria = Criteria::where('SiteTree_Title')
    ->startsWith('prefix')
    ->endsWith('suffix');
$query->addFilter('title-partial-match', $criteria);

// Nested criteria
$topLevel = Criteria::where('SiteTree_ParentID')
    ->is(0);
$criteria = Criteria::where('SiteTree_Title')
    ->startsWith('test')
    ->andWhere($topLevel);
$query->addFilter('top-level-test-pages', $criteria);

Support our work

Buy Me a Coffee at ko-fi.com