How to implement searchable and filterable tags/taxonomy for blog pages in Sanity?
Great question about implementing tags and taxonomy for your blog! There are a couple of common approaches for making blog posts searchable and filterable by tags in Sanity:
Approach 1: Simple Array of Strings
The simplest approach is to use an array of strings directly in your blog post schema:
{
name: 'post',
type: 'document',
title: 'Blog Post',
fields: [
{
name: 'title',
type: 'string',
title: 'Title'
},
{
name: 'tags',
type: 'array',
title: 'Tags',
of: [{type: 'string'}],
options: {
layout: 'tags'
}
}
// other fields...
]
}This gives you a nice tag input interface in the Studio. To filter by tags in GROQ:
*[_type == "post" && "javascript" in tags]Approach 2: Reference-Based Taxonomy (Recommended for Complex Cases)
For more sophisticated taxonomy where you want to manage tags/categories as their own documents (with descriptions, slugs, etc.), use reference fields:
// Tag document type
{
name: 'tag',
type: 'document',
title: 'Tag',
fields: [
{name: 'title', type: 'string'},
{name: 'slug', type: 'slug'},
{name: 'description', type: 'text'}
]
}
// Blog post with tag references
{
name: 'post',
type: 'document',
title: 'Blog Post',
fields: [
{
name: 'tags',
type: 'array',
title: 'Tags',
of: [{
type: 'reference',
to: [{type: 'tag'}]
}]
}
]
}Then query with the -> operator to follow references:
*[_type == "post" && references(*[_type == "tag" && slug.current == "javascript"]._id)]
{
title,
tags[]->
}The reference approach is powerful because:
- Tags become reusable across your content
- You can query bidirectionally (find all posts with a tag, or find all tags used by posts)
- You can add metadata to tags themselves
- It prevents typos and maintains consistency
Filtering in Your Frontend
Once you've structured your tags, you can use GROQ to filter content. For example, to get all posts tagged with specific categories:
*[_type == "post" && count((tags[]->slug.current)[@ in ["javascript", "react"]]) > 0]Both approaches support the searchable/filterable functionality you're looking for - the choice depends on whether you need simple tagging or a more robust taxonomy system!
Show original thread10 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.