๐Ÿ”ฎ Sanity Create is here. Writing is reinvented. Try now, no developer setup

Querying archived posts in Sanity.io causing unexpected results

13 replies
Last updated: May 9, 2023
So I am running into what I think is an odd issue where my query looks like this
export const allPosts = groq`*[_type == "post" && isArchived != true]{
As when I inspect my post I see
and I don't want to display posts that are listed as archived. However, this still shows that post. If I update the query to
export const allPosts = groq`*[_type == "post" && isArchived == false]{
or even

export const allPosts = groq`*[_type == "post" && !isArchived]{
the posts are hidden that are listed as archived
May 9, 2023, 2:45 PM
Which API version?
May 9, 2023, 2:46 PM
isArchived != true
will also match any post that does not have the
attribute, or where it's null.
May 9, 2023, 2:47 PM
I have
export const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2023-01-01'
And that is fine if it matches posts that don't have
or it's null. However, my issue is that it was still matching posts that had it set to
May 9, 2023, 2:49 PM
I don't know what API version that is, just make sure you are using
May 9, 2023, 2:50 PM
That is odd, if so. This is an extremely well-tested type of query. Can you reproduce it with a single query URL or using the Vision plugin?
May 9, 2023, 2:51 PM
Oh I have always see a full date used for
๐Ÿค” even in
user G
tutorial she says you can put any date you want basically. The vision plugin works as expected but the API version shows
But examples like
this show the
May 9, 2023, 2:55 PM
Yes, you need a full date. I mean you need an API version that starts with 2022 or 2023.
May 9, 2023, 2:56 PM
๐Ÿคฆโ€โ™‚๏ธ the problem was me ๐Ÿ˜…So I have

      type: 'boolean',
      name: 'isArchived',
      title: 'Archive Post?',
      description: 'Archived posts will not be shown in the front-end',
      initialValue: false,
where the initial value
should be false but somehow my editor got a state where the boolean is right in the middle and that was the one that kept showing up when
isArchived !== true
but disappearing when I did
May 9, 2023, 3:06 PM
What would be the safer bet? Just to do
May 9, 2023, 3:06 PM
I think that's clean
May 9, 2023, 3:15 PM
Same, I just don't know how the post got into a null state as when I created a new post it was in the false state and even toggling it, it still did not get into the null state ๐Ÿค”
May 9, 2023, 3:16 PM
If you want to handle the null state too, there's nothing wrong with
!= true
either. ๐Ÿ™‚
May 9, 2023, 3:16 PM
Yea that might be safer, my client is a senior citizen building a website for senior citizens in our city so whatever I can do to make it as bullet proof as possible I want to do.
May 9, 2023, 3:18 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?