Reference

Guide

$pages->filter()

Filters elements by a custom filter function or an array of filters

$pages->filter(array|\Closure $filter): Kirby\Cms\Pages

Parameters

Name Type Default Description
$filter array|Closure

Return type

Kirby\Cms\Pages

Exceptions

Type Description
Exception

if $filter is neither a closure nor an array

Example

// fetch children with a title starting with 'Project'
$items = $page->children()->filter(function ($child) {
  return str::startsWith($child->title(), 'Project');
});

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

// fetch visible children which have visible children
$items = $page->children()->listed()->filter(function ($child) {
  return $child->hasListedChildren();
});

More information

Filtering compendium

Inherited from

Kirby\Toolkit\Collection

Source code

kirby/src/Toolkit/Collection.php#L247