GROQ query with slug parameter returning null despite matching document
Looking at your queries, I can spot the issue! In your first query, you're filtering for _type == "blogs" (plural), but in your result data, the actual _type value is "blog" (singular), not "blogs".
Here's what's happening:
Your first query:
*[_type == "blogs"]This is returning results, which suggests you might have some documents with _type == "blogs", but the example result you showed has _type: 'blog' (singular).
Your second query:
*[_type == "blogs" && slug.current == $slug][0]This returns null because there's no document with both _type == "blogs" AND slug.current == "another-blog".
The fix:
Change your second query to match the actual type in your data:
export const postQuery = `*[_type == "blog" && slug.current == $slug][0]`;Also, you should probably update your first query for consistency:
export const postsQuery = `*[_type == "blog"]`;The query syntax itself is correct - you're properly using GROQ parameters with the $slug syntax, and passing the parameter correctly. The issue is just the type mismatch between what you're querying for ("blogs") and what actually exists in your data ("blog").
If you're still getting null after this change, double-check:
- The exact value of
_typein your Sanity Studio schema - That the slug value you're passing (
'another-blog') exactly matches what's in the document (it looks correct from your example)
Show original thread2 replies
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.