Question about optimization of fetching content from another subfolder

Hello everyone,

I’m looking the way to optimize fetching content, currently I use following snippet to fetch the pages from another (not child) subfolder:

$transitcode = $data->transitcode();
foreach($page->site()->index()->filterBy('template', 'document')->filterBy('documentidentifiercode', '*=', $transitcode) as $item): ?>

Since I know that all the pages containing “documentidentifiercode” are comming from /database folder, is there a way to modify the snippet to focus filtering only in /database subfolder instead of plowing all the subfolders?

It’s probably better to do $site->index()->visible->filterby(), or do it in a collection so you can re-use it. You could probably loose the first filterby() too cos i’m guessing the second filterby() is picking up on something only the articles you want contain, so checking the template too seems like a wasted effort.

In general its better to start with minimal set of pages before you start filtering and sorting on it, so if you can reduce the list somehow upfront, it’ll be faster from there.

Is this page at the top level? it can be shortend more if so… $pages->find('databasepage')->children()....

Even shorter

$items = page('database')->children()->filterBy('template', 'document')->filterBy('documentidentifiercode', '*=', $transitcode);

I’d define the collection in the current page’s controller and pass to the snippet. Filtering by template before filtering by the field could maybe be a performance improvement if there are subpages with multiple possible templates in the database folder, because for the template filtering, Kirby doesn’t have to read the content files first.

So, as a general rule:

  • limit set as much as possible and try to avoid searching the complete site index
  • filter first by everything that doesn’t involve reading the content files (visible/invisible, template etc.)
  • filter by content last

Hallo @texnixe!

When I tried this I got an error, what stupid thing I’ve done this time? :slight_smile:

$transitcode = $data->transitcode();
$item = page('database')->children()->filterBy('template', 'document')->filterBy('documentidentifiercode', '*=', $transitcode);
foreach($item): ?>

Where is your page located and what is the error message. page('database') only works for first level page, so you would have to use the full path to the page.

Oh, and it should be $items in your code and then

foreach($items as $item):

not foreach($item)

The page structure is somewhere similar to this

- database
- page that pull info from database

Both folders are on same level.

Please check what I added above and what the error message is and where the error is thrown.

I still don’t know where in the tree your page is, first level in content?


The documents with template “document” are all stored in the “datenbank” using template “database”. The subfolders in “3-Prozessgruppen” should be pulling content from “datenbank” via the snippet we discussed above.

Ok, thank you. Then it should be page('datenbank'), not page('database'),

But looks like you don’t want to tell me what the error message says, so were are moving in circles…

Now I got it - it works! I’ve falsely used template name instead of folder name for page(’…’) :slight_smile:

Another small adition for me, using the filter method in order @texnixe adviced to use lead to huge performance boost for my project and especialy in the panel.