
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storePerfect! You're on the right track. To exclude the current document from your related posts query, you need to filter out documents where the _id matches the parent document's _id.
Here's how to adjust your query:
"related": *[
_type == "post"
&& count(categories[@._ref in ^.^.categories[]._ref]) > 0
&& _id != ^.^._id
] | order(publishedAt desc, _createdAt desc) [0..5] {
title,
slug
}The key addition is && _id != ^.^._id. Let me break down what's happening:
^.^._id uses the parent operator to traverse up two levels to access the _id of the original document you're querying@ operator in categories[@._ref in ^.^.categories[]._ref] represents each item in the current document's categories array as it's being evaluated^.^ is needed because you're inside a projection (the {} part) which is itself nested inside the filterSo the filter reads: "Give me posts that share categories with the parent document, but exclude the parent document itself by checking that _id doesn't match."
This is a common pattern when building "related content" features, and comparing _id values is the most reliable way to exclude the current document since _id is unique across all documents.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store