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?

  • I can see you edited my original question but this is not what I asked.
    – Vin
    Commented May 15, 2018 at 2:37

1 Answer 1


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() %} 
  • I am super ignorant on this. Not sure still how I structure things. Typically I would use {% set entries = craft.entries.section('flightcompany').limit(5) %} but I need to make sure there is a match from the matrix fields partsModels.model = "cessna" and partsModels.part = "rotor56a" that returns only entries with those matches. I am sure I am not explaining it right but hoping so.
    – Vin
    Commented May 14, 2018 at 23:50
  • You won't receive entries when you query for Matrix blocks. To be honest: I have no idea what your issue is. Fetching elements in this way is so trivial and easy so I was still not able to figure out your problem. You can use the related to functionality for Matrix blocks like for entries. Commented May 14, 2018 at 23:55
  • The issue really is, being able to grab data from Entries but only those matching matrix fields. It does seem trivial but I don't want to pull Entries in that don't have those rows that match. If so, I can limit 5 a row just pulling in Entries but not all 5 of them will have those particular matches. I just can't figure it out and maybe it's a lost cause for me.
    – Vin
    Commented May 15, 2018 at 0:02
  • Robin, have you ever done anything like I am talking about without using RelatedTo. From what I can tell after testing is that craft.matrixBlocks.all() does not do anything that I could not do by performing a loop after craft.entries tag. So this has nothing to do with filtering results on the top level from the craft.entries tag via matrix fields. Unless you can explain it, I don't see the relevance of your answer.
    – Vin
    Commented May 15, 2018 at 1:55
  • 1
    Yes it makes sense... That is what you should have asked from the very beginning instead of your explanation about 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 Commented May 15, 2018 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.