Increase performance of query

instead of “filtering” you could do the reverse and keep a “lookup-table” which you keep up-to-date when page objects change.

  1. on each page update use the hook to write the page id into a cached array. one array for each category or only for ‘allwoechentlich-belangloses’ if thats the only one you need. add the aggregated week number as well
  2. instead of performing the query do load the cached array for that category and make a page collection from the stored ids filtered by week
// 1 create a plugin with a cache
// 2 add a hook and write lookup-table to cache
Kirby::plugin('myplugin.createme', [
   'options' => ['cache' => true],
   'hooks' => [
        'page.update:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
            if($page->template()->name() == 'yourchoice') {
                $idAndWeeks = kirby()->cache('myplugin.createme')->get('allwoechentlich-belangloses', []);
                $idAndWeeks[$newPage->id()] = DtuiHelper::getWeekFromDate($newPage->date());
                kirby()->cache('myplugin.createme')->set('allwoechentlich-belangloses', $idAndWeeks);
            }
        }
    ]
]);
// 3 finally read cache and get pages
$idAndWeeks = kirby()->cache('myplugin.createme')->get('allwoechentlich-belangloses', []);
// if array is empty you need to perform the query once and store it. the cache was flushed (or there are no results)
if (count($idAndWeeks) === 0) {
    $idAndWeeks = page('blog')
      ->grandChildren()->children()
      ->filterBy('categories', 'allwoechentlich-belangloses' , ',')
      ->toArray(fn($p) => DtuiHelper::getWeekFromDate($p->date());
    // array of $id => $week pairs
    kirby()->cache('myplugin.createme')->set('allwoechentlich-belangloses', $idAndWeeks);
}
foreach($idAndWeeks as $pageid => $pageweek) {
   if ($week !== $pageweek) continue;
   $thisWeekInThePast[] = $pageid;
}
$thisWeekInThePast = Pages::factory($thisWeekInThePast); // array to pages collection