Is it possible to query elements of type MatrixBlock
in Twig like other elements? You can get entries with
{% set entries = craft.entries.all() %}
Is there something similar available for Matrix blocks?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityIs it possible to query elements of type MatrixBlock
in Twig like other elements? You can get entries with
{% set entries = craft.entries.all() %}
Is there something similar available for Matrix blocks?
Yes you can. craft.entries
returns an object of type EntryQuery
There is a function matrixBlocks
in the CraftVariable that returns an object of type MatrixBlockQuery
Both of them have nearly the same functionality since they extend the ElementQuery
{% set allMatrixBlocks = craft.matrixBlocks.all() %}
craft.entries
and all those things. You can attach a normal andWhere
to your query and follow these rules yiiframework.com/doc/guide/2.0/en/db-query-builder#where. {% set entries = craft.entries.andWhere(['like', 'content.fields_myFieldHandle', 'foo']).all() %}
will search for all entries with a field myFieldHandle
contains foo
. You can as well join your matrix table to the query and look for certain values in it. Please ask a new question for that
– Robin Schambach
May 15 '18 at 6:16