How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Button to link to most recent blog post (index 0)

23 repliesLast updated: Nov 30, 2025

Is there a way I can create a button that would take me to index 0 of my blog, for instance I would want this button to always link to the most recent blog
AI Update

Yes, you can definitely create a button that always links to your most recent blog post! Here's how to approach this:

Using GROQ to Get the Most Recent Post

You'll want to write a GROQ query that fetches blog posts, sorts them by date, and grabs the first one. Here's an example:

*[_type == "blog"] | order(publishedAt desc)[0]

This query:

Once you have the most recent post, you can use its slug field to create your link. Here's a more complete query that projects just what you need:

*[_type == "blog"] | order(publishedAt desc)[0]{
  "slug": slug.current,
  title
}

Then in your frontend code (using Next.js as an example):

import Link from 'next/link'
import { client } from '@/sanity/lib/client'

async function LatestBlogButton() {
  const latestPost = await client.fetch(
    `*[_type == "blog"] | order(publishedAt desc)[0]{ "slug": slug.current }`
  )
  
  return (
    <Link href={`/blog/${latestPost.slug}`}>
      View Latest Post
    </Link>
  )
}

Important Notes

This is a common pattern in Sanity and works great for "Latest Post" links, featured content, and similar use cases!

Show original thread
23 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