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?
With the rest API, you can add a query to the url params
/api/posts?where[status][equals]=publishedFYI, Payload has some built in draft functionality that you might want to check out!
https://payloadcms.com/docs/versions/overview#collection-configBut what can I do if I don’t want give users access to resource (posts) if it don’t have published status?
Use access control on the collection:
read: ({ req: { user } }) => {
if (user) {
return true;
}
return {
or: [
{
status: {
equals: 'published',
},
},
{
status: {
exists: false,
},
},
],
};
},🙂
I try it and write to here by result, thank you 🙏
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
Discord
online
Get dedicated engineering support directly from the Payload team.