👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

hello guys, I noticed that the data I fetch from my sanity studio to my front end are displayed randomly and I don't want that, I want to display the last item I added to...

4 replies
Last updated: Dec 20, 2020
hello guys, I noticed that the data I fetch from my sanity studio to my front end are displayed randomly and I don't want that, I want to display the last item Iadded to be the first item that shows up, how can I do that? I checked the sort documentation and nothing
😞I thought to do a datetime type and set it to the moment the post created by default and sorted by the publish date , but i didn't know how , i just need to display the lastest posts i added , i know this question seems easy to answer but i am new and i will love any kind of help
❤️ thank you
Dec 20, 2020, 3:29 PM
All created documents do have an _updatedAt and _createdAt field by default. You could use them in your fetch query to sort your posts. Here is an example with GraphQL:
Sorry for the quality but it’s still readable I guess
Dec 20, 2020, 4:46 PM
that's great! I did not know that, thank you! do you have any idea how to do it via GROQ?
Dec 20, 2020, 4:59 PM
I haven’t really worked with GROQ yet but reading its docs your query should look like this (once again sorry for the quality):

Dec 20, 2020, 5:07 PM
Thank you so much you saved my life! ❤️
Dec 20, 2020, 5:20 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?