See Sanity in action 👀 Join us for a live product demo + Q&A →

How to Expand a Reference on the Object Level

6 replies
Last updated: Jan 11, 2023
Hello all, does anyone know why
*[_type == 'hos_chapter' && references(@->_id)][0].slug.current
wouldn't work? I've tried
too but no joy. Full code...
Jan 9, 2023, 1:10 PM
"relHos": relHos[]{
      "id": @->_id,
      "title": @->title,
      "status": @->meta.status,
      "slug": @->slug.current,
      @->_type == 'hos' => {
        "chapter": *[_type == 'hos_chapter' && references(@->_id)][0].slug.current
Jan 9, 2023, 1:11 PM
is returning
Jan 9, 2023, 1:16 PM
Also, just to confirm, hardcoding a relevant id does return the chapter, so its something to do with the
that's causing the issue.
Jan 9, 2023, 1:27 PM
It’ll probably be more performant if you expand the reference on the object level:
  "status": meta.status,
  _type == "hos" => {
    "chapter":  *[_type == "hos_chapter" && references(^._id)]
I’d think in this case that
should work for you, however you may have to go up another level (
) because of the conditional.
Jan 10, 2023, 9:47 PM
user M
- I did actually implement that technique afterwards, not sure why I had it like that initially. And thanks, all sorted and working now 😊
Jan 11, 2023, 9:50 AM
Maybe changed things around when you were debugging? I’ve done that before! 🙂
Jan 11, 2023, 3:56 PM – 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.

Categorized in