Add custom object to Sanity GROQ query results before orderBy?

12 replies
Last updated: Jan 28, 2026
is it possible to add a custom record/object into a query result...I'm building an index of pages for a "resource center" page and at least one of the items that I need in this index lives in a hard-coded Remix route. Currently, I'm doing the sorting in the groq query itself, so I was hoping I could just inject a new item and then use orderBy before sending that to my Remix loader. Otherwise, I'll have to do some array manipulation first before sending it to the client, which isn't the end of the world, but I'm just curious.
AI Update

Unfortunately, you cannot inject custom objects directly into a GROQ query result and sort them together with your Sanity documents. GROQ parameters (using $ syntax) only support JSON literal values - they can't be used to merge custom objects into query results. The spread operator (...) in GROQ is specifically for field projections in objects, not for array concatenation or manipulation.

Since GROQ can't handle this, you'll need to do the array manipulation in your Remix loader code instead. Here's a clean approach:

// In your Remix loader
export async function loader() {
  const query = `*[_type == "resource"] | order(title asc)`;
  const sanityResources = await client.fetch(query);
  
  const customResource = {
    _type: 'resource',
    title: 'Hard-coded Route',
    slug: { current: '/your-remix-route' },
    // other fields matching your resource structure
  };
  
  // Combine and sort
  const allResources = [...sanityResources, customResource]
    .sort((a, b) => a.title.localeCompare(b.title));
  
  return json({ resources: allResources });
}

Alternative approaches to consider:

  1. Create it as an actual Sanity document: Even if it's for a hard-coded route, you could create a resource document in Sanity and just reference the static route in the slug field. This keeps all your sorting logic in GROQ and makes it easier for content editors to manage.

  2. Use a singleton document: If this is a one-off special item, you could create a singleton document in Sanity that represents this resource, giving you full GROQ control.

  3. Handle it in your frontend: If sorting client-side is acceptable, you could fetch resources via GROQ and inject/sort the custom item in your React component.

The JavaScript array manipulation approach isn't the end of the world - it's actually quite common when you need to mix CMS content with application-level data. Just make sure your custom object matches the same shape as your Sanity documents so everything works consistently downstream.

Show original thread
12 replies

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?