Collections

Filtering

You can filter collection items by adding a filter key to the collection's array in config.php, and specifying a callable that accepts the collection item and returns a boolean. Items that return false from the filter will not be built.

A common use for filtering is to mark some blog posts as published, using a variable in the YAML front matter of each collection item that specifies a boolean or a date. Using a filter in config.production.php, draft posts can be made visible in the local or staging environments, but omitted from your production build.

config.php

1<?php
2 
3return [
4 'collections' => [
5 'posts' => [
6 'filter' => function ($item) {
7 return $item->published;
8 }
9 ],
10 ],
11];