How to tag and filter blog pages in Sanity.io

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.
Apr 13, 2021, 2:06 PM
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
πŸ˜„
Apr 13, 2021, 2:14 PM
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
Apr 13, 2021, 2:17 PM
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
Apr 13, 2021, 2:20 PM
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
Apr 13, 2021, 2:21 PM
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?
Apr 13, 2021, 2:21 PM
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
Apr 13, 2021, 2:26 PM
Great Thanks! you guys are very helpful and fast to reply.
Apr 13, 2021, 2:27 PM
Happy to help πŸ˜„
Apr 13, 2021, 2:27 PM
I will let you know if I have any more questions. Thanks again
Apr 13, 2021, 2:28 PM
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
Apr 13, 2021, 2:29 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?