Get full collection inside another query

default discord avatar
reepicheep05last year
7

Is there a way to create a field that returns a full collection? I have a

Page

collection and a

Blog

collection in the CMS. Each page entry has a dropdown where I can choose a

Blog

post category and display the posts. I am trying to get the full

Blog

feed in the return from the

Page

query.



I know I can just query the feed in the component, but I was wondering if there is a way to do this in the first

Page

query.



Thanks!

  • default discord avatar
    noheadphoneslast year

    You can use a relationship field, choose a blog post. Then when you query your page just make sure you set your

    depth

    to a higher number, maybe 2 or 3 but it will depend on your data structure on blogs

  • default discord avatar
    reepicheep05last year

    Hey thanks paul, I am acutally trying to get all the blog post feed. I was thinking that I could use the select field to choose the feed (eg. blog posts, case studies etc)

  • default discord avatar
    noheadphoneslast year

    Ohhh I see, via the REST API, this is two separate queries right now



    If you want to make things easier for yourself you could set up a custom REST endpoint



    https://payloadcms.com/docs/rest-api/overview#custom-endpoints


    So you could set up a custom one to get a feed, then inside your handler you would do the logic of fetching the target Page's category and then do a find for blog feed

  • default discord avatar
    reepicheep05last year

    Got it. Thanks Paul!

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.