get only published items returned from a collection using the relationship field

default discord avatar
olarssony
6 months ago
3

like the title says, how can i use the relationship field to only select a collection item that has a status of published? i dont want users of the cms to be able to pick drafts.

  • discord user avatar
    tylandavis
    Payload Team
    6 months ago

    Hey @olarssony , to do this, just pass a query constraint to the

    filterOptions

    property on your relationship field config:


    {
      name: "relatedPosts",
      type: "relationship",
      relationTo: "posts",
      filterOptions: () => {
        return {
          _status: {
            equals: 'published'
          }
        }
      },
    },


    You can find more about the

    filterOptions

    and creating queries here:


    https://payloadcms.com/docs/fields/relationship#filtering-relationship-options
  • default discord avatar
    olarssony
    6 months ago

    cheers! thanks man

  • discord user avatar
    tylandavis
    Payload Team
    6 months ago

    no problem! glad I could help.

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.