<p>Is there a way to get a list of categories with the number of entries associated with each particular category?</p>
<p>I want to output something like this:</p>
<p><a href="https://i.sstatic.net/sxKdz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/sxKdz.png" alt="number of hits per category" /></a></p>
<p>It would make sense to fetch the number of hits in the same DB query. But currently I get the hits by running a <code>craft.products(itemParams).total()</code> per category iteration.</p>
<p>Or is this not a real performance problem?</p>

CC BY-SA 4.0
2020-12-14T15:21:37.563Z
I'm guessing these totals will change depending on filters, so caching's not an option?
dmatthams
CC BY-SA 4.0
2020-12-14T19:02:50.860Z
Yup, but I am using query string parameters, so each unique filter combination is cacheable. Am using caching now. Just asking more out of curiosity as to whether I can do these kinds of queries.
nitech
CC BY-SA 4.0
2020-12-15T10:29:49.987Z
Do you have multiple categories per products?
Oli
CC BY-SA 4.0
2020-12-15T10:58:38.640Z
Yes, and multiple product types
nitech
<categories><performance>
Get categories with number of hits (entries, products)
# Get categories with number of hits (entries, products)

(Asked by: username_0 on 2020-12-14)

Is there a way to get a list of categories with the number of entries associated with each particular category?

I want to output something like this:

[![number of hits per category](https://i.sstatic.net/sxKdz.png)](https://i.sstatic.net/sxKdz.png)

It would make sense to fetch the number of hits in the same DB query. But currently I get the hits by running a `craft.products(itemParams).total()` per category iteration.

Or is this not a real performance problem?

**Tags:** categories, performance

> **Comments:**
> **username_1**: I'm guessing these totals will change depending on filters, so caching's not an option?  
> **username_0**: Yup, but I am using query string parameters, so each unique filter combination is cacheable. Am using caching now. Just asking more out of curiosity as to whether I can do these kinds of queries.  
> **username_2**: Do you have multiple categories per products?  
> **username_0**: Yes, and multiple product types  

---

## 0 answers

