Structured Content 2022: Join our conference to explore fresh perspectives on content and digital experiences →

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.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.