๐Ÿ”ฎ Sanity Create is here. Writing is reinvented. Try now, no developer setup

How to display articles by category in a main index page efficiently.

4 replies
Last updated: Jul 25, 2022
hello there.. i have a main index page which is going to display articles that are written in sanity studio. i want to display articles by category. will i need to call separate queries for each category just to separate the articles out or is there a better way of doing it to avoid 4+ separate queries per page load?
Jul 25, 2022, 6:15 PM
You could do a single query with one to four subqueries, too (either treating them collectively like an array or busting them out for granular changes).
I would query the categories themselves that match the category document type, and then do a projection inside them called something like "posts" that is another query referencing the "parent" category.

Part of the reason is, you may run into situations where you want them to do mostly the same thing but treat them differently (e.g. you need ten total but want to make sure no more than 4 come from Category B)

Alternately, you could also query all posts matching those categories but I'd be worried it's literally grabbing all of them first under the hood before filtering. I try to think general to specific.

If I couldn't make the query itself work, I'd leave it to my language of choice fetching the data to map them out and sort them.

It's not a focus of my talk Wednesday but in the project I'll be discussing I needed to do that at one point; I had a date field for magazine publication dates and developed an array of years from every doc, boiling it down to uniques. Then, whenever the year in the doc matched the array key, in it went, like mugs in a cupboard.

Side note that the
groq channel is well-suited to ask too, just because they're so fluent and clever. I personally have to pseudocode the queries first to understand what I am trying to do haha ๐Ÿ˜ƒ
Jul 25, 2022, 8:16 PM
thanks! yeah i cant cound how many times i have used console.log to see what im doing. it helps a lot! I will just grab all the articles for now and manipulate it myself for now thanks for the help!
Jul 25, 2022, 8:27 PM
In another doc type for departments for career opportunities, I am using the ordering plugin to sort them, and each contains a list of open job positions. That way it "goes into the camera" already in the order I'd like, and is pre-selected.
Probably a lot of work for posts (unless it was a means of treating it like 'featured' posts' but that's a much more concise and efficient query with just a smidge more manual work and then no more elbow grease in the code.
Jul 25, 2022, 8:41 PM
Oh yay, I am pleased with myself that I sort of had the right idea -- I just saw
user L
respond in the other channel ๐Ÿ˜ƒ Editing the top of my initial response for posterity.
Jul 25, 2022, 8:44 PM

Sanityโ€“ build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?