Discussion of filtering related documents in a dynamic filter for an array of references in Sanity.io

12 replies
Last updated: Dec 23, 2022
Hey all 👋I’m working on a dynamic filter for a field which is an array of references, using a property from the surrounding document, but I’m not getting any results at the moment.
I have an
artist
document, inside which is a
selectedExhibitions
field, which is an array of references to
exhibition
documents. I would like to filter the exhibitions in the search by exhibitions that include the
_id
of the artist document that we’re currently in. i.e., only exhibitions which that artist took part in should show up. An exhibition can include more than one artist.
inside
artist.js

    {
      name: 'selectedExhibitions',
      title: 'Selected exhibitions',
      type: 'array',
      description: 'Related exhibition pages',
      of: [
        {
          type: 'reference',
          to: { type: 'exhibition' },
          options: {
            filter: ({document}) => {
                return {
                  filter: '$id in artists._ref',
                  params: {
                    id: document._id,
                }
              }
            }
          }
        }
      ]
    },
inside
exhibition.js

   {
      name: 'artists',
      title: 'Artist(s)',
      type: 'array',
      of: [{ type: 'reference', to: { type: 'artist' } }]
   },
Grateful if anyone can suggest where I might be going wrong.
Dec 18, 2022, 1:05 AM
Hey
user C
! Is there a reason you have the Exhibition in the Artist schema? Or is this for displaying the related Exhibitions for an Artist in a UI? These are two different things.
I would not include Exhibitions in the Artist schema. Instead I’d have Artist as a reference for an Exhibition. Since the Artist exists with or without the Exhibition.

When it comes time to display an Artist detail page, you can pull in the Exhibitions through the relationship established in the Exhibition schema. Relationships flow both ways.
Dec 19, 2022, 2:27 PM
Hey
user J
thank you very much for your reply and thinking this through with me! I can explain. I do definitely see your reasoning and ordinarily exhibitions would be separate from the artist schema. In this case, exhibitions are used within the artist schema because the designer would like to feature related exhibitions inside the individual artist pages in the UI.
At first I thought I could just pull through the exhibitions in the frontend using the artist reference like you described, but the client wants some editorial control over which exhibitions are highlighted on the page in that related section. So my thinking was to provide an array field where those
exhibitions related to the artist have already been filtered and can be selected as desired. Does that seem like a sensible approach? Ideally perhaps I would create a custom input component that could fetch all those related exhibitions and be already populated (I made a start on that but as I have not done that before it may take me a while to figure out that next level solution 😅 — hence for now just having a filtered search would be cool)
Dec 19, 2022, 8:58 PM
Hmmm… Highlighted exhibits for the artist makes sense. But that recursive relationship seems to create complexity in the code that is beyond my knowledge. I’ll raise this with our engineers.
Dec 19, 2022, 9:34 PM
That’s good to hear 🙂 No problem, I could also post in another channel if you prefer? Thank you again for your help
user J
Dec 19, 2022, 9:40 PM
This is the right channel. But since I answered originally, our engineers might have skipped over it.
Dec 19, 2022, 9:42 PM
user M
were you looking into this?
Dec 21, 2022, 3:35 PM
Yep! But quickly got overwhelmed with tickets. I'll take a look again today!
Dec 21, 2022, 4:49 PM
swarm
Dec 21, 2022, 4:49 PM
Seems busier than ever in the help channel these days
user M
! So totally understand you all needing some time to look into things 🙏 No pressure :)
Dec 21, 2022, 10:29 PM
Thanks for your patience Henry and sorry this got lost in the shuffle. This should work for you:
{
      name: 'selectedExhibitions',
      title: 'Selected exhibitions',
      type: 'array',
      description: 'Related exhibition pages',
      of: [
        {
          type: 'reference',
          to: {type: 'exhibition'},
          options: {
            filter: ({document}) => {
              const id = document._id.includes('drafts.') ? document._id.slice(7) : document._id
              return {
                filter: '$id in artists[]._ref',
                params: {
                  id,
                },
              }
            },
          },
        },
      ],
    },
Your previous filter needed the
[]
operator on the
artists
field to iterate over the array. We also needed to add a check for if the document was a draft, and if so, remove
drafts.
from it.
Dec 21, 2022, 11:29 PM
This is very generous thank you
user M
it works perfectly! Client definitely happy with that. Thanks again for the tireless help
Dec 23, 2022, 2:07 AM
Very happy to help!
Dec 23, 2022, 2:08 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?