Sorting based on number of relations entry?

System Information
  • Strapi Version: 3.6.x
  • Operating System: big sur
  • Database: pg
  • Node Version: 14
  • NPM Version: -
  • Yarn Version: 1.22

Hello,

I have a category content type. One category can have multiple article. When i query the categories, i want to sort them by the number of articles inside a category. In the afterFind lifecycle hook, i have added an “on the fly” field in the result witch is: count, to know how much article inside the category has. My problem is that, i also have to sort these by this count field. I could just simply sort the result in this lifecycle hook, but then it will affect every rest call. Is there is a way to do this by the built in filters / sortings?


2 Likes

Have you found the solution yet?

3 Likes