✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

How to Filter from Properties of Expanded References

1 replies
Last updated: Mar 25, 2021
Does anyone know how to filter from properties of expanded references? For instance, I can get all recipes that have a
publishDate
earlier than the current time:

*[
  _type == "recipeArticle"
  && (!defined(releaseDate) || releaseDate <= $currentDate)
] | order(coalesce(releaseDate, _createdAt) desc) {
    title,
    otherRecipeFields
  }[$first..$last]
But I can't figure out how to filter by that
releaseDate
when these articles are references:

  *[_id == "allRecipesPage"]{
    editorsPicks {
      recipes[!defined(releaseDate) || releaseDate <= $currentDate]->{
        ${recipeTeaserGroq}
      },
    },
  }
That filter doesn't work, because it's only looking at the
ref
objects, which of course do not have their own
releaseDate
Mar 25, 2021, 9:43 PM
Nevermind, figured it out:

  *[_id == "allRecipesPage"]{
    editorsPicks {
      recipes[]->[
         !defined(releaseDate) ||
         releaseDate <= $currentDate
      ]{
        ${recipeTeaserGroq}
      },
    },
  }
Mar 25, 2021, 10:00 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?