<p>Yes, you can do that with <a href="https://craftcms.com/docs/4.x/matrix-blocks.html#querying-matrix-blocks" rel="nofollow noreferrer">matrix block queries</a>. Instead of going through entries to get the matrix blocks, you can query matrix blocks independently with a custom sort order.</p>
<pre class="lang-twig prettyprint-override"><code>{% set notesByDate = craft.matrixBlocks()
    .type('note')
    .orderBy('note_date ASC')
    .all()
%}
</code></pre>
<p>Replace <code>note</code> with the handle of the matrix block type of your notes and <code>note_date</code> with the handle of the date field.</p>
<p>This will give you a list of matrix blocks. From the matrix blocks, you can get the owner entry using <a href="https://docs.craftcms.com/api/v4/craft-elements-matrixblock.html#method-getowner" rel="nofollow noreferrer">MatrixBlock::getOwner()</a>:</p>
<pre class="lang-twig prettyprint-override"><code>{% for note in notesByDate %}
    Date: {{ note.date | date('medium') }}
    Note: {{ note.note }}
    Entry: {{ note.getOwner().title }}
{% endfor %}
</code></pre>
<h2>Limiting matrix blocks to specific section</h2>
<p>You can't limit a matrix block query to a specific section directly since they don't belong to a section. Matrix blocks are owned by entries and those entries belong to a section. So you need to limit the matrix query to matrix blocks that are <a href="https://craftcms.com/docs/4.x/matrix-blocks.html#ownerid" rel="nofollow noreferrer">owned by</a> the entries in that section:</p>
<pre class="lang-twig prettyprint-override"><code>{% set entriesInSection = craft.entries().section('myCustomSection').ids() %}
{% set notesByDateInCustomSection = craft.matrixBlocks()
    .type('note')
    .orderBy('note_date ASC')
    .ownerId(entriesInSection)
    .all()
%}
</code></pre>

CC BY-SA 4.0
2022-06-29T14:28:01.583Z
Brilliant - just what I needed. Thanks!
Ed Hebert
CC BY-SA 4.0
2022-06-29T15:36:55.347Z
While this got me 99% of the way there, I use this particular matrix block in several sections of the site. I want to filter the output to results from a particular section. I found that Matrix Blocks to not accept a .section() parameter. Any way to filter these matrixBlock results by section?
Ed Hebert
CC BY-SA 4.0
2022-06-30T07:40:44.153Z
@EdHebert I've added an example for limiting the query to a specific section!
MoritzLost
CC BY-SA 4.0
2022-06-30T13:29:39.347Z
thanks so much - worked!
Ed Hebert
CC BY-SA 4.0
2022-06-29T13:34:32.520Z
2022-06-30T07:38:26.003Z
2022-06-30T07:38:26.003Z
MoritzLost
<p>I have a section with a matrix field that collects multiple notes for each entry, each marked by the date the note is entered. Conceptually, the data is organized within the entries as:</p>
<pre><code>Entry A
     Jan 1     Note
     Mar 1     Note

Entry B
     Feb 1     Note
     Apr 1     Note
</code></pre>
<p>I would like to extract all the notes from all the entries, and then display the collected information sorted by the dates the notes were entered, as in:</p>
<pre><code>Jan 1    Note     Entry A
Feb 1    Note     Entry B
Mar 1    Note     Entry A
Apr 1    Note     Entry B
</code></pre>
<p>Is this possible in Craft? I'm aware of the SuperSorter plugin, but am not sure whether what I am explaining is possible. If it is, I'm unclear about the syntax required to construct the query properly.</p>
<p>Thanks</p>

<matrix><datetime><sorting><sort>
Sorting all matrix fields from multiple entries as one list
# Sorting all matrix fields from multiple entries as one list

(Asked by: username_0 on 2022-06-29)

I have a section with a matrix field that collects multiple notes for each entry, each marked by the date the note is entered. Conceptually, the data is organized within the entries as:
Entry A
     Jan 1     Note
     Mar 1     Note

Entry B
     Feb 1     Note
     Apr 1     Note

I would like to extract all the notes from all the entries, and then display the collected information sorted by the dates the notes were entered, as in:
Jan 1    Note     Entry A
Feb 1    Note     Entry B
Mar 1    Note     Entry A
Apr 1    Note     Entry B

Is this possible in Craft? I'm aware of the SuperSorter plugin, but am not sure whether what I am explaining is possible. If it is, I'm unclear about the syntax required to construct the query properly.
Thanks

**Tags:** matrix, datetime, sorting, sort


---

## 1 answers

### Answer by username_1 on 2022-06-29. Score: 3 | Accepted answer

Yes, you can do that with matrix block queries. Instead of going through entries to get the matrix blocks, you can query matrix blocks independently with a custom sort order.
{% set notesByDate = craft.matrixBlocks()
    .type('note')
    .orderBy('note_date ASC')
    .all()
%}

Replace note with the handle of the matrix block type of your notes and note_date with the handle of the date field.
This will give you a list of matrix blocks. From the matrix blocks, you can get the owner entry using MatrixBlock::getOwner():
{% for note in notesByDate %}
    Date: {{ note.date | date('medium') }}
    Note: {{ note.note }}
    Entry: {{ note.getOwner().title }}
{% endfor %}

Limiting matrix blocks to specific section
You can't limit a matrix block query to a specific section directly since they don't belong to a section. Matrix blocks are owned by entries and those entries belong to a section. So you need to limit the matrix query to matrix blocks that are owned by the entries in that section:
{% set entriesInSection = craft.entries().section('myCustomSection').ids() %}
{% set notesByDateInCustomSection = craft.matrixBlocks()
    .type('note')
    .orderBy('note_date ASC')
    .ownerId(entriesInSection)
    .all()
%}

> **Comments:**
> **username_0**: Brilliant - just what I needed. Thanks!  
> **username_0**: While this got me 99% of the way there, I use this particular matrix block in several sections of the site. I want to filter the output to results from a particular section. I found that Matrix Blocks to not accept a .section() parameter. Any way to filter these matrixBlock results by section?  
> **username_1**: @EdHebert I've added an example for limiting the query to a specific section!  
> **username_0**: thanks so much - worked!  

---

