Sure. There are many ways of getting a similar result. I just like to be concise. And my method produces a kirby collection while yours is a regular array. Functionally equivalent to your method would be:
@walkerbox: call_user_func_array and array_map are new to me - the more I get better at this stuff the more i know I don’t know ~ I misunderstood your first post.
I’m not sure I understand the difference between a kirby collection and an array - does this just mean that there are methods I can use on a collection, such as $collection->method()?
Oh, man. You are going to have so much fun! Also check out array_walk() and array_filter() if you’re interested in native methods for handling arrays. Finally, it’s worth knowing that kirby has $pages->filter() and $pages->map() available.
Works like a charm! Here’s the basic use I’ve made using your solution
// Get a checkbox field content (after basic cleaning) from a page and put it in an array.
$pages_to_fetch = str_replace(' ', '', $pages->find('pagename')->checkboxfield());
// Put the URIs in an array.
$uris = explode( ',', $pages_to_fetch);
// Retrieve the collection of pages using the URIs
$collection = call_user_func_array(array($pages, 'find'), $uris);
// Get page data or children data (that's what I wanted).
foreach ($collection as $fetchedpage) {
echo $post->title();
// I guess there's way to call this within the call_user_func_array() above?
foreach ($fetchedpage->children() as $post){
echo $post->uri()
}
}
Hi, this topic is kinda old but I have a similar problem:
I use the great multiselect plugin to select pages from a query.
The pages are saved like checkboxes values (slug1, slug2, slug3).
I tried $uris = $page->multiselectfield()->split()
which gives me an array of the selected slugs.
Now I tried to use $collection = call_user_func_array(array($pages, 'find'), $uris);
to make a collection.
But obviously that didn’t work as foreach($collection->children() as $post)
gives me no results.
I also tried the method from above (by tasinttttttt) but it didn’t give me any results either.
PS: Please use Markdown code blocks when posting multiline code in the future. They use three backticks at the start and end on separate lines. I have changed it for you above.
Just for information:
I found a solution that looks cleaner and easier to me, now I don’t loop through a collection, but through the array itself and inside the loop I use the ‘uid’ that the array returns to get a page. (I now also use $page->index() instead of $page->children().
If you only need to search within the children of $pages, it makes more sense to use $pages->children() instead of $pages->index() because the last option goes looks in the complete tree, not just the child pages and is therefore less performant.
BTW. code blocks are created with three backticks without a space between them.