šŸ‘‹ Next.js Conf 2024: Come build, party, run, and connect with us! See all events

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]{
  ${postFields}
}`
As when I inspect my post I see
isArchived:true
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]{
  ${postFields}
}`
or even

export const allPosts = groq`*[_type == "post" && !isArchived]{
  ${postFields}
}`
the posts are hidden that are listed as archived
May 9, 2023, 2:45 PM
Which API version?
May 9, 2023, 2:46 PM
Also,
isArchived != true
will also match any post that does not have the
isArchived
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
isArchived
or it's null. However, my issue is that it was still matching posts that had it set to
true
.
May 9, 2023, 2:49 PM
I don't know what API version that is, just make sure you are using
v2022*
or
v2023*
.
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
apiVersion
šŸ¤” 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
v2021-10-21
šŸ¤”
But examples like
this show the
apiVersion
as
2022-11-15
šŸ¤”
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

   defineField({
      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
!isArchived
šŸ˜…
May 9, 2023, 3:06 PM
What would be the safer bet? Just to do
!isArchived
?
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?