πŸ‘€ See Sanity in action: Watch product demo now β†’

I'm having a bit of trouble querying my 'photo' content type by their category. maybe there's a better way to apply category/taxonomy sorting to content?

11 replies
Last updated: Sep 9, 2020
I'm having a bit of trouble querying my 'photo' content type by their category. I tried to mimic the schema from the ecommerce sample sanity supplies, but maybe there's a better way to apply category/taxonomy sorting to content? Below are the two schemas and my query
//Photo Content Schema
export default {
    title: 'Photo',
    name: 'photo',
    type: 'document',
    fields: [
        {
            title: 'Name',
            name: 'name',
            type: 'string'
        },

        {
            title: 'Image',
            name: 'image',
            type: 'image'
        },
        {
            name: 'category',
            title: 'Category',
            type: 'array',
            of: [
                {
                    type: 'reference',
                    to: { type: 'category' }
                }
            ]
        },
    ]
}

//Category Schema
export default {
    name: 'category',
    title: 'Category',
    type: 'document',
    fields: [
        {
            name: 'title',
            title: 'Title',
            type: 'string'
        },
        {
            name: 'slug',
            title: 'Slug',
            type: 'slug',
            options: {
                source: 'title',
                maxLength: 96
            }
        },
    ]
}

//GROQ Query
*[_type == 'photo']{
    _id, name, image,
    category ->
}
Sep 9, 2020, 7:34 PM
Try
category[]->
since it's an array you need th brackets
Sep 9, 2020, 7:51 PM
user J
same result unfortunately
Sep 9, 2020, 7:55 PM
Which is?
Sep 9, 2020, 7:55 PM
query does not return any category info, only id, name, and image
Sep 9, 2020, 7:56 PM
here's any example response
0:{
    "_id":"17bd2de6-1dfe-4bb9-942c-dee4d6faac10"
    "image":{
        "_type":"image"
        "asset":{...}
    }
    "name":"Landscape-4"
}
Sep 9, 2020, 7:58 PM
If you change the query to just
*[_type == 'photo']
Sep 9, 2020, 8:00 PM
What does it return?
Sep 9, 2020, 8:00 PM
Figured out the issue, I was calling 'category' instead of 'categories'. I changed the name, and haven't deployed the changes yet!
Sep 9, 2020, 8:03 PM
✨
Sep 9, 2020, 8:04 PM
Figured out the issue, I was calling 'category' instead of 'categories'. I changed the name, and haven't deployed the changes yet!
Sep 9, 2020, 8:03 PM
✨
Sep 9, 2020, 8:04 PM

Sanity.io – build remarkable experiences at scale

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