Relation field count & ordering by relation count

scenario - there is an Article content type. Each article has a relation to the Likes table (1-N - Article belongs to many Likes). When I query for articles, I want to get the number of likes for each article. Furthermore, I want to be able to sort by the count of likes (thus querying “The most liked…”).

This is a super common scenario I guess, but I don’t see an easy way in Strapi to achieve this. Even in v4, there’s no count metadata for relation fields.

So what’s the best approach to be able to easily:

  1. get number of likes related to the Article
  2. sort articles by the number of likes



you can count the number of related entities like this:

but I don’t know how to sort :frowning:

1 Like

can u teach me how to do add this into my code?

Use this inside a controller or service

Is there a good solution for this? @KadlinoBIT, I’m just curious what you ended up doing.

Have you found a solution? i have a similar problem

1 Like


Did you find any solution for the sort issue ?