How to return title instead of value from string/list in Sanity?

5 replies
Last updated: Mar 15, 2021
Super basic one, need to return the
title
and not the
value
of a
string/list
but it appears way harder than it should be... I must be missing something?
AI Update

Based on the documentation, I can see this is a common point of confusion! When you define a field with type: 'string' and a list of options in your schema, Sanity stores only the value, not the title/label.

The key thing to understand is that the title in list options is purely for the Studio UI - it's what editors see when selecting options, but it never gets saved to your database. Only the value is stored.

Here's an example schema:

{
  name: 'status',
  type: 'string',
  options: {
    list: [
      {title: 'In Progress', value: 'in_progress'},
      {title: 'Completed', value: 'completed'}
    ]
  }
}

When an editor selects "In Progress", your database stores "in_progress".

To display the title in your frontend, you have a few approaches:

  1. Create a mapping function in your frontend code:
const statusTitles = {
  'in_progress': 'In Progress',
  'completed': 'Completed'
}

const displayTitle = statusTitles[document.status]
  1. Use GROQ's select() function in your query to map values to titles:
*[_type == "myDoc"] {
  statusTitle: select(
    status == "in_progress" => "In Progress",
    status == "completed" => "Completed",
    "Unknown"
  )
}
  1. Switch to objects instead of strings if you need both stored:
{
  name: 'status',
  type: 'object',
  fields: [
    {name: 'title', type: 'string'},
    {name: 'value', type: 'string'}
  ]
}

This is mentioned in the preview configuration documentation - titles in list options are display-only and not stored in the database. Most developers go with option 1 or 2 since it keeps your data clean and normalized.

Show original thread
5 replies
*[_type == "document" && _id == '12345'][0]{
    'breaksOver': brk_breaksover
}
I'm afraid the title is not recorded but just there for the select options in the studio. The value is what's actually set on the field.
If you need an actual title/value combination in your data, you could consider creating a custom input component that sets both the title and the value or using a reference field to a separate document type that holds the different options. The advantage of the last approach is that you can easily update titles and values at any time. The downside is having an extra document type just for this purpose.
Ahh okay, that is a little awkward... assumed this would have been an obvious capability. Maybe I missed that in the docs? It would be a helpful to have a note if it's not already there. Will check alternatives, though will likely just get creative on the front-end. Thanks for letting me know! πŸ€™
Getting creative on the front-end sounds like a good way to take care of this as well πŸ˜‰ Noted your docs idea, thanks!

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?