Array Filters
Filters for manipulating arrays, collections, and lists.
A list prints nothing in {{ }}: the filters that return a list give a value to loop over or to pass on, for example to join. The comments below show that value.
first, last, length, reverse and slice work on strings too: {{ 'abc'|first }} gives a.
Accessing Elements
| Filter | Arguments | Description |
|---|---|---|
first | — | Get first element |
last | — | Get last element |
length | — | Get count |
keys | — | Get array keys |
values | — | Get array values |
{{ posts|first }}
{{ posts|last }}
{{ posts|length }}
{{ data|keys }}
{{ data|values }}
Joining
| Filter | Arguments | Description |
|---|---|---|
join | glue, and | Join elements into string |
list | first_delimiter, second_delimiter | Human-readable list |
join Arguments:
glue— Separator between items (default:'')and— Separator before last item (optional)
{{ categories|join(', ') }} <!-- A, B, C -->
{{ categories|join(', ', ' and ') }} <!-- A, B and C -->
{{ tags|join(' | ') }}
list Arguments:
first_delimiter— Separator between items (default:',')second_delimiter— Separator before last item (default:' and')
{{ names|list }} <!-- A, B and C -->
{{ names|list(';', ' or') }} <!-- A; B or C -->
As in Timber, list adds a space after each separator.
Slicing
| Filter | Arguments | Description |
|---|---|---|
slice | start, length, preserve_keys | Extract portion |
batch | size, fill, preserve_keys | Group into batches |
slice Arguments:
start— Start index (default: 0)length— Number of items (optional)preserve_keys— Keep original numeric keys (default: false); text keys are always kept
{{ items|slice(0, 3) }} <!-- First 3 items -->
{{ items|slice(2) }} <!-- Skip first 2 -->
{{ items|slice(-2) }} <!-- Last 2 items -->
batch Arguments:
size— Items per batch (default: 1)fill— Value to fill incomplete batch (optional)preserve_keys— Keep original keys (default: true, as in Twig)
{{ items|batch(3) }} <!-- [[1,2,3], {3:4, 4:5, 5:6}] -->
{{ items|batch(3, null, false) }} <!-- [[1,2,3], [4,5,6]] -->
{{ items|batch(3, 'empty') }} <!-- Fill with 'empty' -->
Keys are kept by default, so the second batch starts at key 3, not 0: pass false as the third argument to number each batch from 0, or loop over each batch rather than reading row[0].
Sorting
| Filter | Arguments | Description |
|---|---|---|
sort | arrow | Sort array |
reverse | preserve_keys | Reverse order |
shuffle | — | Randomize order |
{{ numbers|sort }}
{{ items|sort((a, b) => a.date <=> b.date) }}
{{ items|reverse }}
{{ items|shuffle }}
sort keeps each item's key, as in Twig: [3, 1, 2]|sort gives {1: 1, 2: 2, 0: 3}, so a key-value loop over it still reads the original keys. Add |values to number the items from 0 again, for example before json_encode.
Transformation
| Filter | Arguments | Description |
|---|---|---|
filter | arrow | Keep matching elements |
map | arrow | Transform each element |
reduce | arrow, initial | Reduce to single value |
find | arrow | Find first matching element |
column | name, index | Extract column |
merge | array | Merge arrays |
array | — | Ensure value is array |
filter
Keep elements where the arrow function returns true. The kept elements keep their keys, as in Twig: add |values to number them from 0 again.
{{ items|filter(p => p.active) }}
{{ posts|filter(p => p.meta('featured')) }}
{{ items|filter }} <!-- Remove falsy values -->
{{ [1, 2, 3, 4]|filter(v => v > 2)|values }} <!-- [3, 4] -->
map
Transform each element.
{{ posts|map(p => p.title) }}
{{ items|map(i => i.name) }}
reduce
Reduce array to single value.
{{ items|reduce((carry, item) => carry + item.price, 0) }}
{{ numbers|reduce((a, b) => a + b, 0) }}
find
Find first element the arrow function matches.
{{ items|find(i => i.id == 5) }}
{{ posts|find(p => p.meta('featured')) }}
column
Extract column values (like PHP's array_column).
{{ users|column('name') }} <!-- ['John', 'Jane'] -->
{{ users|column('name', 'id') }} <!-- {1: 'John', 2: 'Jane'} -->
merge
Merge two arrays.
{{ categories|merge(tags) }}
array
Ensure value is array (useful for safe looping).
{{ value|array }} <!-- Wraps non-array in array -->
WordPress Filter
| Filter | Arguments | Description |
|---|---|---|
wp_list_filter | args, operator | Filter using wp_list_filter() |
Arguments:
args— Filter criteria as objectoperator—'AND'(default),'OR', or'NOT'to keep the items that match none of the criteria
{{ posts|wp_list_filter({'post_status': 'publish'}) }}
{{ items|wp_list_filter({'active': true, 'featured': true}, 'AND') }}
Common Patterns
Comma-separated list
{{ post.categories|map(c => c.name)|join(', ') }}
First 3 featured items
{{ posts|filter(p => p.meta('featured'))|slice(0, 3) }}
Count matching items
{{ posts|filter(p => p.post_status == 'publish')|length }}
One field of each row
Repeater and group fields return a list of hashes; column picks one field of each:
{{ post.meta('team')|column('name')|join(', ') }}
Check if array contains item
{{ 'admin' in user.roles ? 'Admin' : 'User' }}