Get all levels of children under one parent

I’m looking for a clean way to get all levels of children (children, grandchildren, great-grandchildren, etc) under a specified parent as one group of pages that I can then run a search on.

Alternative, is an easy way to tell the search function to only return items under one or more specific parents without having to loop through them all and checking for the top-level parent.

Maybe I’m looking at this the wrong way, but in looking through search documentation and such I haven’t found anything that looked very clean for doing what I need to do.

Basically, end result in Kirby 2 is to feed it a list of only the specific parents I want to search under (or only the specific parents I don’t want to search under, either way).

$page->index() is what you are looking for, I think: http://getkirby.com/docs/cheatsheet/page/index

So maybe

$results = page('page-A','page-B')->index()->search('term');

According to the docs, $page->search() is supposed to search all descendants of that particular page. So you don’t need the index() part of it.

1 Like

True. I’d suggest changing the example on http://getkirby.com/docs/cheatsheet/page/index in that case, @bastianallgeier.

Ah, OK, now I’m seeing the comments there that it goes through all the subpages of the page being searched. Very nice.

OK, looks like I can then do something like $results = $pages->find('page-A','page-B')->index()->search(get('q'), 'title|text', array('words' => true))->visible(); (the page('page-A','page-B') above didn’t work, and this bit does require the index() in there).

I can’t seem to feed it an array of parent pages, though. Have to manually put them all in the find statement, but I can live with that.

Instead the ->find() part, I believe, you could also filter the $pages collection to check which page is part of your array:

$pages->filter(function($child) {
  return in_array($child->uid(), $YOURARRAY);
})->index()->search();

Depending on what’s actually in your array, you might switch $child->uid()to something else like $page->template()or so.

I’ll give that a shot, thanks. After I figure out these pages that aren’t displaying their content in v2. :-/