Set all subpages to same status as parent

Hello - I am trying to figure out a way to set the child pages of a parent to the same status via the page.changeStatus:after hook but I can’t figure out the logic. Can someone point me in the right direction?

return [
  'hooks' => [
    'page.changeStatus:after' => function ($page) {
      if($page->status() == 'listed') {
        $page->children()->changeStatus('listed');
      }
    }
  ]
];

You have to loop through the children and call changeStatus on a single page object, not on the collection.

Somthing like:

return [
  'hooks' => [
    'page.changeStatus:after' => function ($page) {
      if($page->status('listed')) {
        foreach($page->children() as $item) {
          $item->changeStatus('listed');
        }
      }
    }
  ]
];

Yes, but note that you have to use named parameters $newpage/$oldPage, not $page:

    'hooks' => [
        'page.changeStatus:after' => function ($newPage, $oldPage) {
            if ($newPage->status() === 'listed') {
                foreach ($newPage->children() as $item) {
                    $item->changeStatus('listed');
                }
            }
        }
    ]

Note that you also can’t pass listed as argument to the status() method.