Sanity GROQ query: return pages in the order of items in the array

152 views Asked by At

I am looking to return certain pages where there ID is in an array of page ID's, using the following GROQ query:

*[_type == "page" && _id in ["ea62b903-cbf9-4c47-90b4-a893aecf276e","77eeb0e8-0368-420b-abfa-a8773d064a8c","cb81057d-db1e-4127-bc97-5e3b81aead7d","90bfad56-6396-471e-9db8-2a45f3ff6a79"]]

However, the pages returned in the result are not in the order of page ID's in my array...i.e. the page with the ID 'ea62b903-cbf9-4c47-90b4-a893aecf276e' is not returned first. Does anyone know how to solve this?

1

There are 1 answers

2
Guest On

I would use a dedicated number-type field on each page and then use that field for GROQ ordering. That way you can set up each page's place in the order, something like:

*[_type == "page"] | order(sorting_number asc)

I'm using something like that when I need to allow clients to create and place navigation menu items.

If you have simple pages you can even use a schema "Pages" that has an array of actual pages. That way you can sort pages by dragging them in place with the mouse in Studio, creating the order those pages will be queried.