Get limited number of entries but count and filter all

System Information
  • Strapi Version:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

I’m using the REST API for now, and I have a collection of media, each one having a category.

I would like for strapi to return a limited list of medias so I can paginate them.
But I would also like to display how many total medias there are for each category.
Am I forced to use two queries ?

Since i get only a fraction of the medias I can’t filter them by category to get the totals. I also can’t use count since it only gives me a total and I won’t have a clue which media corresponds to what category.

Thanks for the help :slight_smile:

It should be fine to use two queries. You can also create a custom controller and route to get total count for media based on category id using the count.

Or you can create a custom controller that will inject the count of media items into the category.

Example response may look like the following.

"category": {
      "data": {
        "id": 1,
        "attributes": {
          "name": "action",
          "count": 15,
          "createdAt": "2023-09-14T19:01:22.477Z",
          "updatedAt": "2023-09-14T19:01:24.741Z",
          "publishedAt": "2023-09-14T19:01:24.740Z"
        }
      }
    }