How to implement searchable and filterable tags/taxonomy for blog pages in Sanity?

10 replies
Last updated: Apr 13, 2021
Hey Everyone, I have a question regarding tagging blog pages / taxonomy so that they are searchable and filterable.
AI Update

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]

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 thread
10 replies
Hey Matt! What sort of tagging and taxonomy are you hoping for?
There are a few solid ways of accomplishing this, so I don't want to overwhelm you with options that don't meet your need
πŸ˜„
Hey Bryan - basically we need to tag each blog page with a category and then be able to filter / see a list page of all blog pages with said tags
Here is a screenshot of the design we are working with. Category tag needs to be filterable and clickable to see all blog pages with that tag
Gotcha!
I'd recommend setting up a "Category" document type (to start with it can be as simple as a title, but you might augment it later with additional fields).

Then on your post document type, you'll create an Array field of References, something like this:

{
    title: 'Categories',
    name: 'categories',
    type: 'array',
    of: [
        {
            type: 'reference',
            to: [
                {type: 'category'},
            ],
        },
        
    ],
}
By using References and an external document type, you make it easier to change the category names and data in the future.

Then for querying against them, you can check against that array field and see if a specific category ID is included in the array and return documents for that
Ok great. I am going to pass this information off to my developer. Thank you! And then we can get them filtered into an all / filtered page like this?
Yup! Depending on how your data is set up, you'll run a GROQ query to check posts to see if they contain the ID from the category and then return just those posts
Great Thanks! you guys are very helpful and fast to reply.
Happy to help πŸ˜„
I will let you know if I have any more questions. Thanks again
Yup, if you run into stumbling blocks with querying, reach back out. I was hoping to send an example, but couldn't find a dataset I have that matches the idea of the structure. I can definitely spin one up quickly if y'all get stuck

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.

Was this answer helpful?