Join live – Get insights, tips, + Q&A from Sanity developers on our latest releases

Understanding the usage of `->` in Sanity.io queries for filtering by category or tags in an array.

10 replies
Last updated: Aug 31, 2022
useEffect(() => {
        client.fetch(
            `
            *[_type == "post" && $keyword in categories[]->title] {
                ...,
                categories[] -> {
                        title,
                        slug
                },
              },
                `,{"keyword":"AQIDAH"}
        ).then((data) => setPosts(data)).catch(console.error)
    }, [])
Aug 31, 2022, 9:16 AM
Please kindly group your messages in a single one so we can use a unique thread to discuss your situation.
Aug 31, 2022, 9:40 AM
Otherwise, yes. It’s the right way of using params.
Aug 31, 2022, 9:40 AM
thanks.
Aug 31, 2022, 9:48 AM
I got help from this message. But I am still curious why this will work
[_type == "website" && $keyword in tag[]->name]{tag[]->{name},}
but those not work

[_type == "website" && $keyword in tag[]->name.current]{tag[]->{name}}

[_type == "website" && $keyword in tag[].name]{tag[]->{name}}
I read the document from this

https://www.sanity.io/schemas/sanity-groq-filter-by-category-or-tags-in-array-68dd555e
Aug 31, 2022, 10:21 AM
[_type == "website" && $keyword in tags[].label]{....}

user A
Aug 31, 2022, 10:26 AM
@Muhammad Jamshaid still not workbut Your example work for me ,thank a lot
I just lean sanity second day
Aug 31, 2022, 10:31 AM
wish you good luck mate 😁
user A
Aug 31, 2022, 10:33 AM
->
means “to follow/resolve the reference”. So
tag->name
and
tag.name
don’t mean the same thing.
tag->name
means read the
name
field of the
tag
reference document.
tag.name
means read the
name
field of the
tag
object.
Aug 31, 2022, 10:38 AM
If your
tags
field on the
website
document schema is an array of objects with a
name
property, you need to use
tags[].name
, because you want to read the
name
property of every object in the array.
If your
tags
fields is an array of references to
tag
documents, you need to use
tags[]->name
because you want to read the
name
field of every
tag
document referenced in the array.
Aug 31, 2022, 10:40 AM
@Kitty (they/she) 🏳️‍⚧️ Really helpful! Thank a lot !!!I finally understand how it work , especially
->
means “to follow/resolve the reference”.Yes, My
tag
fields is an array of references to
tags
documents,
Aug 31, 2022, 10:55 AM

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?

Categorized in