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

Query String not Changing Order of Products

4 replies
Last updated: Jan 20, 2023
Hello. I have this query:`const queryCollection = groq``

*[_type in ['collection','subCollection'] && slug.current == $slug][0]

{

title,

description,

image,

subcategories,

subcategories[]->,

'products': *[_type == 'product' && references(^._id)] | order($direction)

}
```
I am using a query string to change $direction based on user input. I load the query into $direction:

client.fetch(queryCollection, {

slug: urlSlug, direction: queryParam

})
But while I see 'queryParam' has the correct value with a console.log (I am alternating between _createdAt desc, price desc, price asc) the order never changes. When I manually place any of these params into the query 'products' sorts correctly. And more interestingly my slug is working as expected. Is there something wrong with the way I am passing my second param?
Jan 20, 2023, 4:53 PM
I got this working by making direction a template literal, which I had previously brought up in this channel. I was trying to avoid this to clean up my syntax. I would like to understand why my previous code didn't work. Is it because $direction is nested in the query?
Jan 20, 2023, 6:38 PM
I don’t think that params get passed to the order function. If I recall correctly, they only work for your filter and projection.
Jan 20, 2023, 7:24 PM
Alright thanks for the info !
Jan 20, 2023, 7:40 PM
You’re welcome!
Jan 20, 2023, 7:41 PM

Sanity.io – 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