Skip to main content

Array Filters

Filters for manipulating arrays, collections, and lists.

Accessing Elements

FilterArgumentsDescription
firstGet first element
lastGet last element
lengthGet count
keysGet array keys
valuesGet array values
{{ posts|first }}
{{ posts|last }}
{{ posts|length }}
{{ data|keys }}
{{ data|values }}

Joining

FilterArgumentsDescription
joinglue, andJoin elements into string
listseparator, andHuman-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

FilterArgumentsDescription
slicestart, length, preserve_keysExtract portion
batchsize, fill, preserve_keysGroup 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

FilterArgumentsDescription
sortcallbackSort array
reversepreserve_keysReverse order
shuffleRandomize order
{{ numbers|sort }}
{{ items|sort((a, b) => a.date <=> b.date) }}
{{ items|reverse }}
{{ items|shuffle }}

Transformation

FilterArgumentsDescription
filtercallbackKeep matching elements
mapcallbackTransform each element
reducecallback, initialReduce to single value
findcallbackFind first matching element
columncolumn_key, index_keyExtract column
mergearrayMerge arrays
arrayEnsure 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

FilterArgumentsDescription
wp_list_filterargs, operatorFilter using wp_list_filter()

Arguments:

  • args — Filter criteria as object
  • operator'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(', ') }}
{{ 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' }}