Matrix Block Queries

You can fetch Matrix blocks in your templates or PHP code using Matrix block queries.

{# Create a new Matrix block query #}
{% set myMatrixBlockQuery = craft.matrixBlocks() %}

Once you’ve created a Matrix block query, you can set parameters on it to narrow down the results, and then execute it by calling .all(). An array of MatrixBlock objects will be returned.

TIP

See Introduction to Element Queries to learn about how element queries work.

Example

We can display content from all the Matrix blocks of an element by doing the following:

  1. Create a Matrix block query with craft.matrixBlocks().
  2. Set the owner, fieldId, and type parameters on it.
  3. Fetch the Matrix blocks with .all().
  4. Loop through the Matrix blocks using a for tag to output the contents.
{# Create a Matrix block query with the 'owner', 'fieldId', and 'type' parameters #}
{% set myMatrixBlockQuery = craft.matrixBlocks()
    .owner(myEntry)
    .fieldId(10)
    .type('text') %}

{# Fetch the Matrix blocks #}
{% set matrixBlocks = myMatrixBlockQuery.all() %}

{# Display their contents #}
{% for block in blocks %}
    <p>{{ block.text }}</p>
{% endfor %}

WARNING

In order for the returned Matrix block(s) to be populated with their custom field content, you will need to either set the fieldId or id parameter.

Parameters

Matrix block queries support the following parameters:

anyStatus

Clears out the status and enabledForSite() parameters.

{# Fetch all Matrix blocks, regardless of status #}
{% set MatrixBlocks = craft.matrixBlocks()
    .anyStatus()
    .all() %}

asArray

Causes the query to return matching Matrix blocks as arrays of data, rather than MatrixBlock objects.

{# Fetch Matrix blocks as arrays #}
{% set MatrixBlocks = craft.matrixBlocks()
    .asArray()
    .all() %}

dateCreated

Narrows the query results based on the Matrix blocks’ creation dates.

Possible values include:

ValueFetches Matrix blocks…
'>= 2018-04-01'that were created on or after 2018-04-01.
'< 2018-05-01'that were created before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were created between 2018-04-01 and 2018-05-01.
{# Fetch Matrix blocks created last month #}
{% set start = date('first day of last month')|atom %}
{% set end = date('first day of this month')|atom %}

{% set MatrixBlocks = craft.matrixBlocks()
    .dateCreated(['and', ">= #{start}", "< #{end}"])
    .all() %}

dateUpdated

Narrows the query results based on the Matrix blocks’ last-updated dates.

Possible values include:

ValueFetches Matrix blocks…
'>= 2018-04-01'that were updated on or after 2018-04-01.
'< 2018-05-01'that were updated before 2018-05-01
['and', '>= 2018-04-04', '< 2018-05-01']that were updated between 2018-04-01 and 2018-05-01.
{# Fetch Matrix blocks updated in the last week #}
{% set lastWeek = date('1 week ago')|atom %}

{% set MatrixBlocks = craft.matrixBlocks()
    .dateUpdated(">= #{lastWeek}")
    .all() %}

fieldId

Narrows the query results based on the field the Matrix blocks belong to, per the fields’ IDs.

Possible values include:

ValueFetches Matrix blocks…
1in a field with an ID of 1.
'not 1'not in a field with an ID of 1.
[1, 2]in a field with an ID of 1 or 2.
['not', 1, 2]not in a field with an ID of 1 or 2.
{# Fetch Matrix blocks in the field with an ID of 1 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .fieldId(1)
    .all() %}

fixedOrder

Causes the query results to be returned in the order specified by id.

{# Fetch Matrix blocks in a specific order #}
{% set MatrixBlocks = craft.matrixBlocks()
    .id([1, 2, 3, 4, 5])
    .fixedOrder()
    .all() %}

id

Narrows the query results based on the Matrix blocks’ IDs.

Possible values include:

ValueFetches Matrix blocks…
1with an ID of 1.
'not 1'not with an ID of 1.
[1, 2]with an ID of 1 or 2.
['not', 1, 2]not with an ID of 1 or 2.
{# Fetch the Matrix block by its ID #}
{% set MatrixBlock = craft.matrixBlocks()
    .id(1)
    .one() %}

TIP

This can be combined with fixedOrder if you want the results to be returned in a specific order.

inReverse

Causes the query results to be returned in reverse order.

{# Fetch Matrix blocks in reverse #}
{% set MatrixBlocks = craft.matrixBlocks()
    .inReverse()
    .all() %}

limit

Determines the number of Matrix blocks that should be returned.

{# Fetch up to 10 Matrix blocks  #}
{% set MatrixBlocks = craft.matrixBlocks()
    .limit(10)
    .all() %}

offset

Determines how many Matrix blocks should be skipped in the results.

{# Fetch all Matrix blocks except for the first 3 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .offset(3)
    .all() %}

orderBy

Determines the order that the Matrix blocks should be returned in.

{# Fetch all Matrix blocks in order of date created #}
{% set MatrixBlocks = craft.matrixBlocks()
    .orderBy('elements.dateCreated asc')
    .all() %}

owner

Sets the ownerId and siteId parameters based on a given element.

{# Fetch Matrix blocks created for this entry #}
{% set MatrixBlocks = craft.matrixBlocks()
    .owner(myEntry)
    .all() %}

ownerId

Narrows the query results based on the owner element of the Matrix blocks, per the owners’ IDs.

Possible values include:

ValueFetches Matrix blocks…
1created for an element with an ID of 1.
'not 1'not created for an element with an ID of 1.
[1, 2]created for an element with an ID of 1 or 2.
['not', 1, 2]not created for an element with an ID of 1 or 2.
{# Fetch Matrix blocks created for an element with an ID of 1 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .ownerId(1)
    .all() %}

ownerSite

Narrows the query results based on the site the owner element was saved for.

This parameter is only relevant for Matrix fields that are set to manage blocks on a per-site basis.

Possible values include:

ValueFetches Matrix blocks…
'foo'created for an element in a site with a handle of foo.
a [Site](api:craft\models\Site) objectcreated for an element in the site represented by the object.
{# Fetch Matrix blocks created for an element with an ID of 1,
   for a site with a handle of 'foo' #}
{% set MatrixBlocks = craft.matrixBlocks()
    .ownerId(1)
    .ownerSite('foo')
    .all() %}

ownerSiteId

Narrows the query results based on the site the owner element was saved for, per the site’s ID.

This parameter is only relevant for Matrix fields that are set to manage blocks on a per-site basis.

Possible values include:

ValueFetches Matrix blocks…
1created for an element in a site with an ID of 1.
':empty:'created in a field that isn’t set to manage blocks on a per-site basis.
{# Fetch Matrix blocks created for an element with an ID of 1,
   for a site with an ID of 2 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .ownerId(1)
    .ownerSiteId(2)
    .all() %}

relatedTo

Narrows the query results to only Matrix blocks that are related to certain other elements.

See Relations for a full explanation of how to work with this parameter.

{# Fetch all Matrix blocks that are related to myCategory #}
{% set MatrixBlocks = craft.matrixBlocks()
    .relatedTo(myCategory)
    .all() %}

Narrows the query results to only Matrix blocks that match a search query.

See Searching for a full explanation of how to work with this parameter.

{# Get the search query from the 'q' query string param #}
{% set searchQuery = craft.request.getQueryParam('q') %}

{# Fetch all Matrix blocks that match the search query #}
{% set MatrixBlocks = craft.matrixBlocks()
    .search(searchQuery)
    .all() %}

site

Determines which site the Matrix blocks should be queried in.

The current site will be used by default.

Possible values include:

ValueFetches Matrix blocks…
'foo'from the site with a handle of foo.
a craft\models\Site objectfrom the site represented by the object.
{# Fetch Matrix blocks from the Foo site #}
{% set MatrixBlocks = craft.matrixBlocks()
    .site('foo')
    .all() %}

siteId

Determines which site the Matrix blocks should be queried in, per the site’s ID.

The current site will be used by default.

{# Fetch Matrix blocks from the site with an ID of 1 #}
{% set MatrixBlocks = craft.matrixBlocks()
    .siteId(1)
    .all() %}

status

Narrows the query results based on the Matrix blocks’ statuses.

Possible values include:

ValueFetches Matrix blocks…
'enabled'(default)that are enabled.
'disabled'that are disabled.
{# Fetch disabled Matrix blocks #}
{% set MatrixBlocks = craft.matrixBlocks()
    .status('disabled')
    .all() %}

type

Narrows the query results based on the Matrix blocks’ block types.

Possible values include:

ValueFetches Matrix blocks…
'foo'of a type with a handle of foo.
'not foo'not of a type with a handle of foo.
['foo', 'bar']of a type with a handle of foo or bar.
['not', 'foo', 'bar']not of a type with a handle of foo or bar.
an MatrixBlockType objectof a type represented by the object.
{# Fetch Matrix blocks with a Foo block type #}
{% set MatrixBlocks = myEntry.myMatrixField
    .type('foo')
    .all() %}

typeId

Narrows the query results based on the Matrix blocks’ block types, per the types’ IDs.

Possible values include:

ValueFetches Matrix blocks…
1of a type with an ID of 1.
'not 1'not of a type with an ID of 1.
[1, 2]of a type with an ID of 1 or 2.
['not', 1, 2]not of a type with an ID of 1 or 2.
{# Fetch Matrix blocks of the block type with an ID of 1 #}
{% set MatrixBlocks = myEntry.myMatrixField
    .typeId(1)
    .all() %}

uid

Narrows the query results based on the Matrix blocks’ UIDs.

{# Fetch the Matrix block by its UID #}
{% set MatrixBlock = craft.matrixBlocks()
    .uid('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
    .one() %}

with

Causes the query to return matching Matrix blocks eager-loaded with related elements.

See Eager-Loading Elements for a full explanation of how to work with this parameter.

{# Fetch Matrix blocks eager-loaded with the "Related" field’s relations #}
{% set MatrixBlocks = craft.matrixBlocks()
    .with(['related'])
    .all() %}