# Count for Tagged Items in a Collection

**URL:** <https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291>\
**Category:** Help\
**Created:** [March 10, 2017, 3:56am UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291 "2017-03-10T03:56:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![timklapdor](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/timklapdor/32/114_2.png) [@timklapdor](https://talk.jekyllrb.com/u/timklapdor)\
**Post date:** [March 10, 2017, 3:56am UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/1 "2017-03-10T03:56:16Z")

</div>

Hi All,

I decided to redo the structure of my site and move some of my Posts to Collections - to be more semantic and friendly. I had a couple an archive page set up that would show posts grouped by Tags. At the top was the list of tags with a post count badge, and below was a list of all the posts under those headings. I’ve managed to recreate the tag lists but I can’t seem to get a collection item count.

The old code used {{ site.tags[tag].size }} - so I’m wondering if there is an equivalent for getting the same information for tags used in collections?

Thanks!

---

<div class="post-metadata">

**Author:** ![timklapdor](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/timklapdor/32/114_2.png) [@timklapdor](https://talk.jekyllrb.com/u/timklapdor)\
**Post date:** [March 10, 2017, 4:15am UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/2 "2017-03-10T04:15:45Z")

</div>

Some more information:

```
{% assign collection_tags = site.my-collection | map: 'tags' | join: ',' | split: ',' | uniq | sort %}

    <ul class="tags">
        {% for tag in collection_tags %}
            <li><a href="#{{ tag | slugify: 'pretty' }}" class="tag">{{ tag }}<span>({{ site.tags[tag].size }})</span></a></li>
        {% endfor %}
    </ul>
```

---

<div class="post-metadata">

**Author:** ![mmistakes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mmistakes/32/2169_2.png) [@mmistakes](https://talk.jekyllrb.com/u/mmistakes)\
**Post date:** [March 10, 2017, 3:57pm UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/3 "2017-03-10T15:57:40Z")

</div>

My memory is hazy on this, but when I tried to do something similar awhile back I don’t think it was possible, though things may have changed.

At the time `site.tags` and `site.categories` were only applied to posts and not available to all collection documents.

---

<div class="post-metadata">

**Author:** ![timklapdor](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/timklapdor/32/114_2.png) [@timklapdor](https://talk.jekyllrb.com/u/timklapdor)\
**Post date:** [March 11, 2017, 11:04pm UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/4 "2017-03-11T23:04:25Z")

</div>

Thanks for the response. That seems to be the case. Can you recommend an alternative way of getting that value? I seem to be a little stuck on this one detail. Is there a way to query how many items each of the tags found in collection\_tags?

---

<div class="post-metadata">

**Author:** ![timklapdor](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/timklapdor/32/114_2.png) [@timklapdor](https://talk.jekyllrb.com/u/timklapdor)\
**Post date:** [March 13, 2017, 11:57am UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/5 "2017-03-13T11:57:26Z")

</div>

I found the following on StackOverflow - [http://stackoverflow.com/questions/36479756/counting-collection-tags-in-jekyll](http://stackoverflow.com/questions/36479756/counting-collection-tags-in-jekyll)

It seems to do the job but is a bit verbose. Seems to be OK as a work around but would love there to be a simpler way.

---

<div class="post-metadata">

**Author:** ![dieghernan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/dieghernan/32/1714_2.png) [@dieghernan](https://talk.jekyllrb.com/u/dieghernan)\
**Post date:** [June 5, 2019, 4:26am UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/6 "2019-06-05T04:26:03Z")

</div>

This solution has been tested in a GitHub-hosted website:

I use Jekyll on my GitHub-hosted page, you can use `group_by` to achieve so:

```
{% assign alldocs = site.COLLECTIONNAME | <additional filtering and sorting> %}	
{% assign grouptag = alldocs | map: 'tags' | join: ',' | split: ',' | group_by: tag %}
{%- for tag in grouptag -%}
    <h1>{{- tag.name -}} - {{tag.size}}</h1>
	{%- for document in alldocs -%}
    	{% if document.tags contains tag.name %}
        	 <p>{{- document.title -}}
		 {% endif %}
    {%- endfor -%}
{%- endfor -%}

```

Live example of a tag cloud including all the content of my blog (posts and projects) [here](https://dieghernan.github.io/tags/)

---

<div class="post-metadata">

**Author:** ![pippim](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/pippim/32/4089_2.png) [@pippim](https://talk.jekyllrb.com/u/pippim)\
**Post date:** [December 14, 2021, 11:41am UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/7 "2021-12-14T11:41:33Z")

</div>

@dieghernan Sorry to say your link is “404”…

---

<div class="post-metadata">

**Author:** ![dieghernan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/dieghernan/32/1714_2.png) [@dieghernan](https://talk.jekyllrb.com/u/dieghernan)\
**Post date:** [December 14, 2021, 3:32pm UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/8 "2021-12-14T15:32:21Z")

</div>

Thank! Should be [Tags | One world](https://dieghernan.github.io/tags)

---

<div class="post-metadata">

**Author:** ![dieghernan](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/dieghernan/32/1714_2.png) [@dieghernan](https://talk.jekyllrb.com/u/dieghernan)\
**Post date:** [February 28, 2025, 12:04am UTC](https://talk.jekyllrb.com/t/count-for-tagged-items-in-a-collection/291/9 "2025-02-28T00:04:45Z")

</div>

Sorry to say this not work any more on Jekyll \>= 4.1.0. I implemented a solution in pure liquid that should work in any version ([link](https://dieghernan.github.io/202502_liquid-group-by/)):

```auto

{% assign alldocs = site.documents %}
{% assign alltags = alldocs | map: 'tags' | join: ',' | split: ',' %}
{% assign single_tags = alltags | uniq %}

<!-- Counting -->
{% assign count_tags = '' | split: ',' %}
{% assign n_tags = single_tags | size | minus: 1 %}
{% for i in (0..n_tags) %}
  {% assign count_this_tag = alltags | where_exp:"item", "item == single_tags[i]" | size %}
  {% assign count_tags = count_tags | push: count_this_tag %}
{% endfor %}

<!-- Extra: sort -->
{% assign items_max = count_tags | sort | last %}
{% assign sorted_tags = '' | split: ',' %}
{% assign sorted_count_tags = '' | split: ',' %}

{% for i in (1..items_max) reversed %}
  {% for j in (0..n_tags) %}
    {% if count_tags[j] == i %}
     {% assign sorted_tags = sorted_tags | push: single_tags[j] %}
     {% assign sorted_count_tags = sorted_count_tags | push: i %}
    {% endif %}
  {% endfor %}
{% endfor %}

{% assign sorted_tags = sorted_tags | uniq %}

 {%- for i in (0..n_tags) %}
      <p>{{ sorted_tags[i] }} - {{ sorted_count_tags[i] }} </p>
  {%- endfor -%}

```
