Skip to content

Dynamic ordering #1090

@ozgurdemir

Description

@ozgurdemir

I'm trying to order a GROQ query dynamically by passing an $order variable.

Hardcoding does work

const query = groq`{"BlogPosts": *[_type=='blog_post'] | order(publishedAt desc)}`;
const { data, status } = await useSanityQuery(query)

Passing a variable does not work. The ordering is just ignored and the results are returned without ordering

const query = groq`{"BlogPosts": *[_type=='blog_post'] | order($order desc)}`;
const { data, status } = await useSanityQuery(query, {
  'order': 'publishedAt',
});

The same when passing the order field as a string. This does not work:

const query = groq`{"BlogPosts": *[_type=='blog_post'] | order('publishedAt' desc)}`;
const { data, status } = await useSanityQuery(query)

Am I passing the order variable in a wrong format or is this not possible with this module?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions