Joint session with Vercel: How to build intelligent storefronts (May 15th)

Add custom object to Sanity GROQ query results before orderBy?

12 repliesLast updated: Dec 2, 2025

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:

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

Was this answer helpful?

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.

Related contributions