<p>You could go via <code>entries()</code> and do something like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% set blockCount = craft.entries()
    .section('yourSectionHandle')
    .with([
        ['matrixFieldHandle', {type: 'blockHandle'}]
    ])
    .all() | map(b =&gt; b.blocks | length) | reduce((carry, v) =&gt; carry + v) %}
</code></pre>
<p>In short, <code>map</code> (<a href="https://twig.symfony.com/doc/3.x/filters/map.html" rel="nofollow noreferrer">docs</a>) builds an array of block count/per entry and <code>reduce</code> (<a href="https://twig.symfony.com/doc/3.x/filters/reduce.html" rel="nofollow noreferrer">docs</a>) sums it all up.</p>
<p>Eager-loading the blocks using <code>with</code> will only return enabled blocks.</p>

CC BY-SA 4.0
2021-07-27T11:35:24.170Z
2021-07-27T11:35:24.170Z
Oli
<p>Trying to grab the total count of matrix blocks that belong to a section. Looks like there is no section parameter for craft.matrixBlocks(). Not sure as to why that is.</p>
<p>Anyone know of another way that isn't convoluted i.e. looping through craft.entries and building array?</p>
<pre><code>{% set totalBlocksEnabled = craft.matrixBlocks()
.status('enabled')
.site(siteHandle)
.type(type)
.field('blocks')
.total() %}
</code></pre>
<p>No section parameter.
<a href="https://craftcms.com/docs/3.x/matrix-blocks.html#querying-matrix-blocks" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/matrix-blocks.html#querying-matrix-blocks</a></p>

<matrix>
craft.matrixBlocks() by section
# craft.matrixBlocks() by section

(Asked by: username_0 on 2021-07-22)

Trying to grab the total count of matrix blocks that belong to a section. Looks like there is no section parameter for craft.matrixBlocks(). Not sure as to why that is.
Anyone know of another way that isn't convoluted i.e. looping through craft.entries and building array?
{% set totalBlocksEnabled = craft.matrixBlocks()
.status('enabled')
.site(siteHandle)
.type(type)
.field('blocks')
.total() %}

No section parameter.
https://craftcms.com/docs/3.x/matrix-blocks.html#querying-matrix-blocks

**Tags:** matrix


---

## 1 answers

### Answer by username_1 on 2021-07-27. Score: 1 | Accepted answer

You could go via entries() and do something like this:
{% set blockCount = craft.entries()
    .section('yourSectionHandle')
    .with([
        ['matrixFieldHandle', {type: 'blockHandle'}]
    ])
    .all() | map(b => b.blocks | length) | reduce((carry, v) => carry + v) %}

In short, map (docs) builds an array of block count/per entry and reduce (docs) sums it all up.
Eager-loading the blocks using with will only return enabled blocks.


---

