πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

How to get the title of a referenced document while keeping all other data in GROQ.

7 replies
Last updated: Jan 28, 2022
When using GROQ, how can I get the title of the document I'm referencing while keep all the other data I'm requesting?I have a list of images that have a few key:value pairs that I'll need, then there is one called
tags
that has a reference to
collections
each image can be tagged with many collections. I want to keep the structure I have and just convert the reference to the actual collection title
I'm sure this is easier than I'm making it, but I can't figure out how to structure my query. I'll post current query along with a screenshot of the data I'm request in the comments
Jan 28, 2022, 7:57 PM
my query
*[_type == "quiz-v2" && setDefault == true][0] {
    design_inspo_builder[3]
	
	 }

Jan 28, 2022, 7:58 PM
what i'm getting back
Jan 28, 2022, 7:58 PM
Correct me if I'm misunderstanding your question, but is your ultimate goal to get all of the fields, as well as the one you're defining in your current query? If so, you can use
...
, like so:
*[_type == "quiz-v2" && setDefault == true][0] {
    ...,
    design_inspo_builder[3]
}
Jan 28, 2022, 8:14 PM
not exactly, I want everything in
design_inspo_builder[3]
, but once I get to the nested tags inside the image, I would also like the title of those tags.
So, I would still have the object as is, but tags would look like

tag:[
{
"_key":"14a797683a38"
"_ref":"7bce91b5-6465-473b-991a-d75ffa7aab7a"
"_type":"reference"
"tag_name": "name of the tag"
}
]
Jan 28, 2022, 8:22 PM
I see now!
*[_type == "quiz-v2" && setDefault == true][0] {
    design_inspo_builder[3] {
      ...,
      image_options {
        images[]{
          ...,
          tags[]->
        }
      }
  }
}
Jan 28, 2022, 8:27 PM
not exactly, I want everything in
design_inspo_builder[3]
, but once I get to the nested tags inside the image, I would also like the title of those tags.
So, I would still have the object as is, but tags would look like

tag:[
{
"_key":"14a797683a38"
"_ref":"7bce91b5-6465-473b-991a-d75ffa7aab7a"
"_type":"reference"
"tag_name": "name of the tag"
}
]
Jan 28, 2022, 8:22 PM
Amazing!!!! Thanks so much!
I was doing something similar, but had left off the array brackets for images and tags and was getting no results
🀦
Jan 28, 2022, 8: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?