How to create a Pages Collection from IDs of draft pages?

I have an array of page IDs like

$pages = [
  'blog/post-one',
  'blog/post-two',
  'blog/post-three',
]

With

$collection = new Pages($pages ?? []);

I receive a Pages Collection $collection containing those three pages. This works fine with pages of status listed or unlisted, but not with drafts; if the IDs in $pages are for pages of draft status, I receive this error:

Kirby\Exception\InvalidArgumentException thrown with message “You must pass a Page object to the Pages collection”

Is there a way to create a pages collection from a set of IDs, while those are drafts?

Maybe using the factory instead? Not sure if it accepts a set of ids as input, though.

1 Like

I tried the factory as well but I could not yet figure out a way to hand it an array of IDs. After a bit of reconsideration, I did achieve what I wanted with the following approach:

$collection = new Pages();
foreach ($pages as $page) {
  $collection->add(kirby()->page($page));
}

Remains to be seen how this performs, but for now it appears a viable solution …and I learned that new Pages() seemingly has the same limitation as the page() helper, to only cover listed and unlisted pages.