How would I filter out drafts in a localapi query?

default discord avatar
banks0
last month
4

Title basically 🙂

  • discord user avatar
    tylandavis
    Payload Team
    last month

    Here's an example for a

    posts

    collection to get only published documents:



    const getPosts = async () => {
      const posts = await payload.find({
        collection: "posts",
        where: {
          _status: {
            equals: "published",
          },
        },
      });
  • default discord avatar
    banks0
    last month

    thanks



    and how would I add another condition to it like

    where: {
        "group.slug": {
          equals: slug,
        },


    -> only return published and in with specific where

  • discord user avatar
    tylandavis
    Payload Team
    last month

    hey @banks0 , sorry for the late reply on this, you can combine multiple query conditions using

    and

    like so:



    where: {
      and: [
        _status: {
          equals: "published",
        },
        "group.slug": {
          equals: slug,
        },
      ],
    }


    You can also use

    or

    as well, which works the same way. That would return documents that satisfy

    either

    condition, wheras

    and

    must satisfy both/all conditions.

    https://payloadcms.com/docs/queries/overview#and-or-logic
  • default discord avatar
    banks0
    last month

    thanks, I appreciate it

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.