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.
Note that by default, variables from environment-specific config files are not merged recursively; only the top-level keys are considered for merging. For collections, you can override this behavior by setting
merge_collection_configs
totrue
in your mainconfig.php
file. This will allow you to only specify the environment-specific changes to yourcollections
settings.
config.php
1<?php 2 3return [ 4 'merge_collection_configs' => true, 5 'collections' => [ 6 'posts' => [ 7 'filter' => function ($item) { 8 return $item->published; 9 },10 // other settings for this collection...11 ],12 // other collections...13 ],14];
config.production.php
1<?php 2 3return [ 4 'collections' => [ 5 'posts' => [ 6 'filter' => function ($item) { 7 return $item; 8 } 9 ],10 ],11];