Is it possible to get a single document from a collection using the slug/id?
I'm using this to get all of the blog-posts, but on the singular blog post page would ideally just get the required one
const getPosts = async () => {
const posts = await payload.find({
collection: 'client-posts',
})
return posts
}
I've tried the following, but it didn't work (
context.params.slug[0]
is definitely valid)
const getPosts = async () => {
const posts = await payload.find({
collection: 'client-projects',
where: {
slug: {
equals: context.params.slug[0],
},
},
})
return posts
}
payload.findById
should do what you're looking for, as long as you have the id (
https://payloadcms.com/docs/local-api/overview#:~:text=%7D)%3B-,Find%20by%20ID,-//%20Result%20will%20be). 👍
Hey @Plain134 - As mentioned by @Tinouti, the Local API operation of Find by ID is what you are looking for. Were you able to get this working?
Let me know!
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.