Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How to filter query by field value

default discord avatar
mozomig3 years ago
5

Hi, how i can filtering query by field value? For example in blog template i have status field


{


name: 'status',


type: 'select',


options: [


{


value: 'draft',


label: 'Draft',


},


{


value: 'published',


label: 'Published',


},


],


defaultValue: 'draft',


admin: {


position: 'sidebar',


}


}


I would like return data in the query only with status Published, how can i do it?

  • discord user avatar
    jarrod_not_jared
    3 years ago

    With the rest API, you can add a query to the url params

    /api/posts?where[status][equals]=published

    FYI, Payload has some built in draft functionality that you might want to check out!

    https://payloadcms.com/docs/versions/overview#collection-config
  • default discord avatar
    mozomig3 years ago

    But what can I do if I don’t want give users access to resource (posts) if it don’t have published status?

  • discord user avatar
    jarrod_not_jared
    3 years ago

    Use access control on the collection:


    read: ({ req: { user } }) => {
      if (user) {
        return true;
      }
    
      return {
        or: [
          {
            status: {
              equals: 'published',
            },
          },
          {
            status: {
              exists: false,
            },
          },
        ],
      };
    },


    🙂

  • default discord avatar
    mozomig3 years ago

    I try it and write to here by result, thank you 🙏

  • discord user avatar
    jarrod_not_jared
    3 years ago

    Yep no problem, you may need to adjust the user check, i.e. if you only want users with a certain role or something to be able to always read posts

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.