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

default discord avatar
olarssonylast year
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
    last year

    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
    olarssonylast year

    cheers! thanks man

  • discord user avatar
    tylandavis
    last year

    no problem! glad I could help.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

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