# How to query frequency of tags via API?

**URL:** <https://community.directus.com/t/how-to-query-frequency-of-tags-via-api/1233>\
**Category:** Help\
**Tags:** querying, api\
**Created:** [September 26, 2025, 9:03am UTC](https://community.directus.com/t/how-to-query-frequency-of-tags-via-api/1233 "2025-09-26T09:03:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![icouto](https://sea2.discourse-cdn.com/flex002/user_avatar/community.directus.com/icouto/32/940_2.png) [@icouto](https://community.directus.com/u/icouto)\
**Post date:** [September 26, 2025, 9:03am UTC](https://community.directus.com/t/how-to-query-frequency-of-tags-via-api/1233/1 "2025-09-26T09:03:00Z")

</div>

I have setup a`posts`collection, with a`tags`field. The most frequently used tags are going to be displayed in a ‘tag cloud’, on a website. I need to be able to query Directus via the REST API, and get a list of all tags, ordered by the most to least frequently used.

I initially tried using a standard ‘tags’ field, but it seems the field stores the tags as a JSON list, and the `aggregate[]`functions cannot be applied to the individual list items.

I then tried to setup a separate `tags`collection, and then setup a many-to-many `tags `field in the `posts`collection. I can now get the count of how many times each tag is being used — by querying the `posts_tags`collection directly — but the result gives me the tag IDs instead of the actual tag\_text, which is in the related `tags `collection. I’ve tried using `?fields=tags_id.tag_text`, but as soon as I add an aggregate or groupBy function, it seems the `fields`parameter gets ignored, and I just get the IDs…

I feel like I’m overlooking something basic — any guidance would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![icouto](https://sea2.discourse-cdn.com/flex002/user_avatar/community.directus.com/icouto/32/940_2.png) [@icouto](https://community.directus.com/u/icouto)\
**Post date:** [September 27, 2025, 12:35am UTC](https://community.directus.com/t/how-to-query-frequency-of-tags-via-api/1233/3 "2025-09-27T00:35:35Z")

</div>

Actually, I worked out a way to do it.

When I created the many-to-many field in the `posts`collection, I didn’t specify that I wanted the equivalent field added to the `tags`collection. As a result, I couldn’t query the `tags`collection for the number of posts connected to each tag — which is what we needed.

I recreated the relationship, now with fields on both sides. I am now able to query the API for the 20 most frequently used tags, through the `tags` collection, like this:`

```auto
GET /items/tags?
    fields=tag_text,count(posts)
    &order=-count(posts)
    &limit=20

```

Directus is **amazing**! 😄

---

<div class="post-metadata">

**Author:** ![ahmad\_quvor](https://sea2.discourse-cdn.com/flex002/user_avatar/community.directus.com/ahmad_quvor/32/310_2.png) [@ahmad\_quvor](https://community.directus.com/u/ahmad_quvor)\
**Post date:** [September 26, 2025, 12:03pm UTC](https://community.directus.com/t/how-to-query-frequency-of-tags-via-api/1233/2 "2025-09-26T12:03:33Z")

</div>

Hey @icouto

When using `aggregate` and `groupBy` in Directus, you’ll typically only get the raw IDs (like `tags_id`) in the result, not the full related data. Unfortunately, expanding fields like `tags_id.tag_text` doesn’t work as expected with aggregation. So yes, after getting the tag IDs and their counts, you’ll need to make a second request to fetch the actual tag text for each ID from the `tags` collection. That’s just how Directus handles relational data in aggregate queries right now.
