Array in sanity-CMS query

How to include an array in Sanity GROQ

Array in a Content model

The ideal use case of an array in Sanity content model is a set of tags for post. In case of tags it can be simple string, for advance use case we can use other content type.

Querying array

Just like other fields we can access the array and loop the object using front end tools you picked.

*[_type == "post"]{title,tags,summary}`

For mapping custom types we can use the following syntax.

*[_type == "post" && slug.current =='${params.slug}']{title,recommended[]->{
    title,
    slug
   }
  }`

In our example every post had a set of recommended posts which is type of posts. To map this we have to specify the object using object syntax.

GROQ
Sanity-cms
How-to

Comments

JhoneyInvalid Date

Useful..

Write your comment