Create page in a hook to a page with status false

I need to create a child page in a hook and set it to a status different to draft. But this child page needs to have status: false because, if the site admin set this page to draft, a section in the panel will break.

Here my hook code.

'page.create:after' => function ($page) {
        if ($page->template() == 'project') {
            $childrenPage = $page->createChild([
                'slug'     => 'data',
                'template' => 'data',
            ]);
            $childrenPage->changeStatus('unlisted');
        }
    },

the docs says this is no possible but is there any workaround?

Thanks in advance

What you could try: Allow changing the status for a single role that you only use for programmatic changes. Then impersonate this user in the hook to perform the status change. Never tested this but worth a try.

Thanks!!! This worked perfectly!
Altough I think it could be handy that when inpersonate(‘kirby’) it truly has all privileges.

That’s true. Have you verified that it doesn’t work?

Yes, didn‘t worked for me