Array Filters
Filters for manipulating arrays, collections, and lists.
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 | separator, and | 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:
separator— Separator between items (default:', ')and— Separator before last item (default:' and ')
{{ names|list }} <!-- A, B and C -->
{{ names|list('; ', ' or ') }} <!-- A; B or C -->
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 keys (default: false)
{{ 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)
{{ items|batch(3) }} <!-- [[1,2,3], [4,5,6]] -->
{{ items|batch(3, 'empty') }} <!-- Fill with 'empty' -->
Sorting
| Filter | Arguments | Description |
|---|---|---|
sort | callback | Sort array |
reverse | preserve_keys | Reverse order |
shuffle | — | Randomize order |
{{ numbers|sort }}
{{ items|sort((a, b) => a.date <=> b.date) }}
{{ items|reverse }}
{{ items|shuffle }}
Transformation
| Filter | Arguments | Description |
|---|---|---|
filter | callback | Keep matching elements |
map | callback | Transform each element |
reduce | callback, initial | Reduce to single value |
find | callback | Find first matching element |
column | column_key, index_key | Extract column |
merge | array | Merge arrays |
array | — | Ensure value is array |
filter
Keep elements where callback returns true.
{{ items|filter(p => p.active) }}
{{ posts|filter(p => p.featured) }}
{{ items|filter }} <!-- Remove falsy values -->
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 matching callback.
{{ items|find(i => i.id == 5) }}
{{ posts|find(p => p.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'
{{ 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.featured)|slice(0, 3) }}
Count matching items
{{ posts|filter(p => p.status == 'published')|length }}
Extract unique values
{{ posts|map(p => p.category)|column('name') }}
Check if array contains item
{{ 'admin' in user.roles ? 'Admin' : 'User' }}