<p>Yes you can. <code>craft.entries</code> returns an object of type <code>EntryQuery</code> </p>

<p>There is a function <code>matrixBlocks</code> in the <a href="https://github.com/craftcms/cms/blob/master/src/web/twig/variables/CraftVariable.php#L288" rel="nofollow noreferrer">CraftVariable</a> that returns an object of type <code>MatrixBlockQuery</code> </p>

<p>Both of them have nearly the same functionality  since they extend the <code>ElementQuery</code></p>

<pre><code>{% set allMatrixBlocks = craft.matrixBlocks.all() %} 
</code></pre>

CC BY-SA 4.0
2018-05-14T23:50:57.360Z
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
CC BY-SA 4.0
2018-05-14T23:55:26.650Z
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.
Robin Schambach
CC BY-SA 4.0
2018-05-15T00:02:00.553Z
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
CC BY-SA 4.0
2018-05-15T01:55:27.120Z
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
CC BY-SA 4.0
2018-05-15T05:19:50.830Z
I don't know what you are talking about but I did many things with craft within the last few years since I spend all in all around 5000h with it. I created my custom relationship parser too. You question was how to fetch matrix blocks like you do it with `craft.entries` (Btw please stop calling it "tag" it has nothing to do with a tag). As I already said you should learn to ask properly. Is your question "how to fetch matrix blocks" or "how to search for entries with certain matrix blocks"
Robin Schambach
CC BY-SA 4.0
2018-05-15T05:58:45.067Z
I have no doubt you know what you are doing, it's just anyone loses perspective when they become good at something they have done over and over.  My question is this..."how to search for entries with certain matrix blocks".   That is more the one.  I want to search entries and have the ability to have a Where clause as a sql would.  I know it's different in Craft but does that make sense to what I am asking?
Vin
CC BY-SA 4.0
2018-05-15T06:16:49.933Z
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 https://www.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
CC BY-SA 4.0
2018-05-15T06:24:16.887Z
That is it Robin, that is the great start of what I am looking for! I will ask the new question about joining the matrix tables.
Vin
CC BY-SA 4.0
2018-05-14T23:05:35.997Z
2018-05-14T23:26:26.217Z
2018-05-14T23:26:26.217Z
Robin Schambach
<p>Is it possible to query elements of type <code>MatrixBlock</code> in Twig like other elements? You can get entries with</p>

<pre><code>{% set entries = craft.entries.all() %} 
</code></pre>

<p>Is there something similar available for Matrix blocks? </p>

CC BY-SA 4.0
2018-05-15T02:37:14.410Z
I can see you edited my original question but this is not what I asked.
Vin
<matrix><craft3><query><elementquery>
Craft query Matrix blocks in twig
# Craft query Matrix blocks in twig

(Asked by: username_0 on 2018-05-14)

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?

**Tags:** matrix, craft3, query, elementquery
> **Comments:**
> **username_0**: I can see you edited my original question but this is not what I asked.  

---

## 1 answers

### Answer by username_1 on 2018-05-14. Score: 3 | Accepted answer

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

> **Comments:**
> **username_0**: 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.  
> **username_1**: 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.  
> **username_0**: 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.  
> **username_0**: 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.  
> **username_1**: I don't know what you are talking about but I did many things with craft within the last few years since I spend all in all around 5000h with it. I created my custom relationship parser too. You question was how to fetch matrix blocks like you do it with `craft.entries` (Btw please stop calling it "tag" it has nothing to do with a tag). As I already said you should learn to ask properly. Is your question "how to fetch matrix blocks" or "how to search for entries with certain matrix blocks"  
> **username_0**: I have no doubt you know what you are doing, it's just anyone loses perspective when they become good at something they have done over and over.  My question is this..."how to search for entries with certain matrix blocks".   That is more the one.  I want to search entries and have the ability to have a Where clause as a sql would.  I know it's different in Craft but does that make sense to what I am asking?  
> **username_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 https://www.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  
> **username_0**: That is it Robin, that is the great start of what I am looking for! I will ask the new question about joining the matrix tables.  

---

