Structured Content 2022: Join our conference to explore fresh perspectives on content and digital experiences →

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: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.