Query entries of several types sorted by field value

Hi,

I’m pretty sure of the answer but I have to ask to make sure I haven’t missed anything.

Summarized question is: is it possible to fetch entries of several different content types sorted by a common field?

I have a category/subcategory hierarchy in which I have a bunch of contents based on different content types and there’s no way around having different content types as content structures are too different.
All of the content types have a common field “pageViews” and are linked to the category and the subcategory via reference fields.
I need to create a category landing page where I’ll list all the subcategories and for each subcategory I need to display the 4 most viewed contents based on the “pageViews” field, whatever the type.

The only solution I found is to make as many requests as I have different content types to consider, to pull the 4 most viewed of each type and merge the list in my component to get the final 4.
This is an easy but really not optimized way to achieve the goal as if I have 3 different content types, I’ll need to make 3 API calls per subcategory.
Let’s say I have 5 subcategories to show, this ends up doing at least 15 (3*5) requests for this page.

I couldn’t find any better way to do this and I’m open to suggestions.
I use GraphQL but if a better alternative is possible using REST, I can work with it.

Thanks for any help you could bring.

Hi @pigoury.wp,

Unfortunately I think your conclusions are correct. At the moment what you mentioned is not possible. A possible workaround would be to maintain a daily local cache of those values whenever the pageView value is either displayed or updated. This would significantly reduce the API calls and you could eventually decrease the cache lifespan (ie: 2-3 hours) to have a good balance between data accuracy and API calls.

Thanks for the reply, sorry I didn’t receive any email notification, I would have replied earlier otherwise :slight_smile:

I didn’t have much hope honestly but I needed to make sure.

1 Like