Skip to content

Kirby 4.1.2

$pages->filter()

Filters elements by one of the predefined filter methods, by a custom filter function or an array of filters

$pages->filter(string|array|\Closure $field, mixed ...$args = null): Kirby\Cms\Pages

Parameters

Name Type Default
$field * string|array|Closure
... $args mixed null

Return type

Kirby\Cms\Pages

This method does not modify the existing $pages object but returns a new object with the changes applied. Learn more →

Parent class

Kirby\Cms\Pages inherited from Kirby\Toolkit\Collection

Example

// fetch children with a title starting with 'Project'
$items = $page->children()->filter(
    fn ($child) => Str::startsWith($child->title(), 'Project')
);

// fetch children with more than 2 images
$items = $page->children()->filter(
    fn ($child) => $child->images()->count() > 2
);

// fetch visible children which have visible children
$items = $page->children()->listed()->filter(
    fn ($child) => $child->hasListedChildren()
);

More information

Filtering compendium