Reference

Guide

$pages->filterBy()

Filters elements by one of the predefined filter methods.

$pages->filterBy(string $field, array $args = null): Kirby\Cms\Pages

Parameters

Name Type Default Description
$field string
$args array null

Return type

Kirby\Cms\Pages

Example

// fetch children with a field 'draft', which has the value 'yes'
$items = $page->children()->filterBy('draft', 'yes');

// fetch children with a date in the past
$items = $page->children()->filter(function ($child) {
  return $child->date()->toDate() < time();
});

// fetch children with a date in the future
$items = $page->children()->filter(function ($child) {
  return $child->date()->toDate() > time();
});

// fetch any page with a project template
$items = $site->index()->filterBy('template', 'project');

// fetch any page with either an article or project template
$items = $site->index()->filterBy('template', 'in', ['article', 'project']);

// fetch children that have the tag 'development'
$items = $page->children()->filterBy('tag', 'development', ',');

// fetch children that have the tag 'development' from a tags field with separator ';'
$items = $page->children()->filterBy('tag', 'development', ';');

Available filter methods

Method Function
== all values that match exactly
!= all values that don't match
in takes an array as parameter, matches all values that are included in the array
not in takes an array as parameter, matches all values that are not included in the array
*= all values that contain the given string
!*= all values that do not contain the given string
^= all values that start with the given string
!^= all values that do not start with the given string
$= all values that end with the given sttring
!$= all values that do not end with a given string
* all values that match the given pattern
!* all values that do not match the given pattern
between all values that match the given range
maxlength all values that match the given maxLength
minlength all values that match the given minLength
maxwords all values that match the given max number of words
minwords all values that match the given min number of words
> all values that are greater than the given value
>= all values that are greater or equal the given value
< all values that are smaller than the given value
<= all values that are smaller or equal the given value

Inherited from

Kirby\Toolkit\Collection

Source code

kirby/src/Toolkit/Collection.php#L256