Joint session with Vercel: How to build intelligent storefronts (May 15th)

Get slug from most recent blog post ordered by date

26 repliesLast updated: Nov 29, 2025

Now my next question is trying to get the slug of the most recent blog article. I want to create a button that when clicked will always take the user to the most recent blog. Not sure what the query would be for this but so far I have
const query = groq `*[_type == "post"][0]{
    "slug": slug->current
}`;
and in my navbar component I'm passing in props which looks like

const Navbar = (props) => {
    const { slug } = props

    return ( some other code...)
}

AI Update

Your query is close, but there are a couple of issues to fix:

Here's the corrected query:

const query = groq`*[_type == "post"] | order(_createdAt desc)[0]{
  "slug": slug.current
}`;

Or if your post schema has a publishedAt or date field (which is more common for blog posts), use that instead:

const query = groq`*[_type == "post"] | order(publishedAt desc)[0]{
  "slug": slug.current
}`;

Breaking it down:

Then in your Navbar component, you can use it like:

const Navbar = (props) => {
    const { slug } = props;
    
    return (
        <Link href={`/blog/${slug}`}>
            Latest Post
        </Link>
    );
}

Note: Make sure the field you're ordering by (_createdAt, publishedAt, etc.) actually exists in your post schema and has values, otherwise the ordering won't work as expected!

Show original thread
26 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions