Confused about collections

So coming from WordPress, I am having some issues/things I don’t understand here. Collections is supposed to work basically as post types in WP? I create a collection, and then I create entries in it. However, in WP i’m used to that each entry has a permalink to access it as a single post page. What I don’t understand is how I with Cockpit and JS can retrieve the data for a single entry, based on a slug for example. Can anyone explain how I would do the following:

  • Page that lists all entries (So far so good).
  • Each entry in the lists links to a single entry href="’/’ + entry.slug’"
  • How do i retrieve the data for this single entry based on a slug or other param?

Your assumption about collections being like post types is correct.

Well, this is one of the points where Cockpit fails to be truly restful.

While you can get all entries using: your.url/api/collections/get/COLLECTIONNAME

You can only seem to get a single collection entry by

fetch('your.url/api/collections/get/COLLECTIONNAME?token=xxtokenxx', {
    method: 'post',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        filter: { '_id': { $in: [1, 4, 5] } },
        limit: 10,
        skip: 5,
        sort: {_created:-1},
        populate: 1 // resolve linked collection items
    })
})
.then(res=>res.json())
.then(res => console.log(res));

Note that filter allows you to filter based on entry ID.

That being said, there should be a restful way as well. I’ll try to see if it isn’t hidden in the API somewhere or if there’s a plugin for it. In any case, it’s not clearly documented.

3 Likes

So, after searching some of the issues on Github, I found this works as well

https://your.url/api/collections/get/COLLECTIONNAME/?filter[{FILTERNAME}]={FILTERVALUE}

example:
https://example.cpm/api/collections/get/posts/?filter[{_id}]={b3f50bbcf7fd9004f3530d7}

3 Likes

Thanks a lot, seems to work well.

do you have a clou how to use multiple filter fields without the json version?

*edit

never mind; found out my self: ?filter[FILTER1]=valX&filter[FILTER2]=valY

1 Like