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 matrixBlocks %}
    <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:

allowOwnerDrafts

Narrows the query results based on whether the Matrix blocks’ owners are drafts.

Possible values include:

ValueFetches Matrix blocks…
truewhich can belong to a draft.
falsewhich cannot belong to a draft.

allowOwnerRevisions

Narrows the query results based on whether the Matrix blocks’ owners are revisions.

Possible values include:

ValueFetches Matrix blocks…
truewhich can belong to a revision.
falsewhich cannot belong to a revision.

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() %}

draftCreator

Narrows the query results to only drafts created by a given user.

Possible values include:

ValueFetches drafts…
1created by the user with an ID of 1.
a \craft\elements\db\User objectby the user represented by the object.
{# Fetch drafts by the current user #}
{% set MatrixBlocks = craft.matrixBlocks()
    .draftCreator(currentUser)
    .all() %}

draftId

Narrows the query results based on the Matrix blocks’ draft’s ID (from the drafts table).

Possible values include:

ValueFetches drafts…
1for the draft with an ID of 1.
{# Fetch a draft #}
{% set MatrixBlocks = craft.matrixBlocks()
    .draftId(10)
    .all() %}

draftOf

Narrows the query results to only drafts of a given Matrix block.

Possible values include:

ValueFetches drafts…
1for the Matrix block with an ID of 1.
a MatrixBlock objectfor the Matrix block represented by the object.
{# Fetch drafts of the Matrix block #}
{% set MatrixBlocks = craft.matrixBlocks()
    .draftOf(myBlock)
    .all() %}

drafts

Narrows the query results to only drafts Matrix blocks.

{# Fetch a draft Matrix block #}
{% set MatrixBlocks = {twig-function}
    .drafts()
    .id(123)
    .one() %}

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.

ignorePlaceholders

Causes the query to return matching Matrix blocks as they are stored in the database, ignoring matching placeholder elements that were set by craft\services\Elements::setPlaceholderElement().

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('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() %}

preferSites

If unique is set, this determines which site should be selected when querying multi-site elements.

For example, if element “Foo” exists in Site A and Site B, and element “Bar” exists in Site B and Site C, and this is set to ['c', 'b', 'a'], then Foo will be returned for Site C, and Bar will be returned for Site B.

If this isn’t set, then preference goes to the current site.

{# Fetch unique Matrix blocks from Site A, or Site B if they don’t exist in Site A #}
{% set MatrixBlocks = craft.matrixBlocks()
    .site('*')
    .unique()
    .preferSites(['a', 'b'])
    .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() %}

revisionCreator

Narrows the query results to only revisions created by a given user.

Possible values include:

ValueFetches revisions…
1created by the user with an ID of 1.
a \craft\elements\db\User objectby the user represented by the object.
{# Fetch revisions by the current user #}
{% set MatrixBlocks = craft.matrixBlocks()
    .revisionCreator(currentUser)
    .all() %}

revisionId

Narrows the query results based on the Matrix blocks’ revision’s ID (from the revisions table).

Possible values include:

ValueFetches revisions…
1for the revision with an ID of 1.
{# Fetch a revision #}
{% set MatrixBlocks = craft.matrixBlocks()
    .revisionId(10)
    .all() %}

revisionOf

Narrows the query results to only revisions of a given Matrix block.

Possible values include:

ValueFetches revisions…
1for the Matrix block with an ID of 1.
a MatrixBlock objectfor the Matrix block represented by the object.
{# Fetch revisions of the Matrix block #}
{% set MatrixBlocks = craft.matrixBlocks()
    .revisionOf(myBlock)
    .all() %}

revisions

Narrows the query results to only revision Matrix blocks.

{# Fetch a revision Matrix block #}
{% set MatrixBlocks = {twig-function}
    .revisions()
    .id(123)
    .one() %}

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.app.request.getQueryParam('q') %}

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

site

Determines which site(s) 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.
['foo', 'bar']from a site with a handle of foo or bar.
['not', 'foo', 'bar']not in a site with a handle of foo or bar.
a \craft\elements\db\Site objectfrom the site represented by the object.
'*'from any site.

TIP

If multiple sites are specified, elements that belong to multiple sites will be returned multiple times. If you only want unique elements to be returned, use unique in conjunction with this.

{# Fetch Matrix blocks from the Foo site #}
{% set MatrixBlocks = craft.matrixBlocks()
    .site('foo')
    .all() %}

siteId

Determines which site(s) 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() %}

trashed

Narrows the query results to only Matrix blocks that have been soft-deleted.

{# Fetch trashed Matrix blocks #}
{% set MatrixBlocks = craft.matrixBlocks()
    .trashed()
    .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() %}

unique

Determines whether only elements with unique IDs should be returned by the query.

This should be used when querying elements from multiple sites at the same time, if “duplicate” results is not desired.

{# Fetch unique Matrix blocks across all sites #}
{% set MatrixBlocks = craft.matrixBlocks()
    .site('*')
    .unique()
    .all() %}

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() %}