Number of posts in Category

Hi.

How can I know the number of posts on the blog page, filtered by category?

I have looked in the forum, an entry that explains how to get the categories of the blog. Now I want to know how many posts are in each category.

Example:
4 posts in category one, 5 posts in category two, etc.

Thanks so much, in advance.

Based on the answer you’ve linked, here’s a quick solution, which I think should work, but not tested

{% set items = page.evaluate({'@page.children': '/items'}) %}
{% set catItemsCounts = [] %}

{% for item in items %}
    {% for cat in item.taxonomy.category %}
        {% set catItemsCounts[cat] = ( catItemsCounts[cat] | default(0) ) + 1 %}
    {% endfor %}
{% endfor %}
1 Like

Hi @Karmalakas

The line {% set catItemsCounts[cat] = ( catItemsCounts[cat] | default(0) ) + 1 %} shows the following error:

Unexpected token "punctuation" of value "[" ("end of statement block" expected).

Apparently Twig doesn’t work like that :slight_smile:
Try this:

{% set catItemsCounts = catItemsCounts|merge({(cat): (catItemsCounts[cat]|default(0)) + 1}) %}

Thanks @Karmalakas.

Your solution works fine as I expected.