Programatically create page with sorting number, directory not named

Hey, I’m working on an import script for blog posts coming from Hugo to Kirby 4. I managed to create posts programmatically and have them show up as “unlisted”. This I believe is because they don’t have any sorting number. When I rename the directory of the post and prefix it with an integer it shows up as published and also updates if I’m changing the order in the panel.

Currently I’m generating the posts with the following code:

                $child = $parent->createChild([
                            'slug'     => $slug,
                            'draft' => $draft,
                            'num'   => $index,
                            'template' => 'post',
                            'content' => [
                            'title'  => $title,
                            'blocks' => json_encode($blocks->toArray()),
                            'date' => $date
                            ]
                ]);

I also tried using the update function like:

                $child->update([
                    'num' => 15,
                ]);

None of these is generating a directory name prefixed by 15 though. What’s the best way to programmatically generate the sorting number?

Thanks!

I figured out that the following works, maybe it helps someone else. I’d still be curious what’s wrong with setting the num directly in the page creation though.

$child->changeStatus('listed', 15);

Set draft to true, then set the num property to something, see Page::create() | Kirby CMS. Not quite clear from your code what you set, as that value is hidden in the $draft variable.

Thanks for your response (and all your other helpful posts that I’ve read during this migration)!

The value of draft is “false” for almost all my entries, as they were already blog posts published with Hugo. Needing to set it to “draft: true” when I want to create a “listed” (public, not draft) page seems a bit unintuitive or am I missing something there?

Thanks!

Indeed, but that’s what seems to be the only thing that works and I think I tested almost all variations.

Calling publish() afterwards works as well, of course.