Generate new pages/folders that sort by date

Hi everyone! I have a question about how to add pages that are prepended with the date of publication (e.g. “02212025_”), rather than the usual “1_” “2_” etc. I have a template, news.yml, that starts like this:

title: News

pages:
  num: date
  template: news

columns:
    - width: 1/2
      sections:
          info:
              type: fields

I thought that the num:date would enable it to generate and sort by date of publication, but when I add a new page with that template, in the finder view the corresponding folder still follows the “1_” “2_” convention. Could anyone let me know what I’m missing here?

The num property belongs on the main level, not under pages. Remove pages and template, and change indentation of num to top level.

Great, that worked, thank you!! However, when I try to get the individual news pages now to show up in reverse chronological order in a “news index” sort of page (with default.txt), it seem my sortBy method is not actually affecting the order in which the pages appear.

This is my default.php:

<?php snippet('header') ?>

<?php 
    $children = $page->children()->listed()->sortBy('date', 'desc');
?>

<header class="header">
    <p>
        <a href="<?= $site->url() ?>"><?= $site->title() ?></a>
    </p>
</header>

<?php foreach ($children as $child): ?>                
    <div>
        <a href="<?= $child->url() ?>">
            <?= $child->title() ?>
        </a>
        <div>
            <?= $child->date()->toDate('Y-m-d') ?>
        </div>
    </div>
<?php endforeach ?>
    
</body>
</html>

Regardless of whether I specify ->sortBy('date', 'desc') or ->sortBy('date', 'asc'), the child pages appear in chronological order, and <?= $child->date()->toDate('Y-m-d') ?> also doesn’t show up, only the title does.

Edit: I’m realizing this is maybe because I don’t have an actual “date” field in my news.yml, the date of publication is only recorded in the folder name itself. In that case is my only recourse to get the pages to show in reverse chronological order specifying $children = $page->children()->listed()->flip();?

If you don’t have a date field, then I wonder why num: date now works, it franky doesn’t make sense without a date field.

Hm, I don’t know! This is the entirety of the code in my news.yml file currently, which is working to automatically prepend the date of publication (i.e., the date I change the page status from draft to public) to my folders:

title: News

num: date

columns:
    - width: 1/2
      sections:
          info:
              type: fields
              fields:
                  Description:
                      label: Project description
                      type: textarea
                      required: false
                  Title:
                      type: text
                      required: true
                  Year:
                      type: text
                      width: 1/2
                  Location:
                      type: text
                      width: 1/2
                  Credits:
                      type: text
                      width: 1/2
    - width: 1/2
      sections:
          gallery:
              headline: Images & Videos
              info: "{{ file.dimensions }}"
              help: Files cannot exceed 10MB in size. Only upload files you want to be seen in the project page, not necessarily on the homepage.
              type: files
              uploads: true
              layout: cards
              template: gallery-item
              image:
                  ratio: 1/1
                  cover: false
              replace: true
              accept:
                  - image/*
                  - video/*

And this is what’s in my default.yml:

title: Default Page

columns:
  main:
    width: 2/3
    sections:
      fields:
        type: fields
        fields:
          text:
            type: textarea
            size: huge
  sidebar:
    width: 1/3
    sections:
      pages:
        type: pages
        # template: project
      files:
        type: files

I see, right, I could verify Kirby simply uses the current date.

But since the number already determines the order in the file system, you probably don’t have to sort and can just flip the order of the listed pages. It will work fine as long as you don’t have to order multiple pages that are published on the same day, where a date field with time would be more convenient. Is there a reason you don’t want to use a date field?

Thank you for clarifying! My main reason for being hesitant about the date field is that the person I’m making this website for wants it to have as few input options as possible (as close to “hassle-free” as you can get), to the extent that they’d likely prefer the date of publication to be automatically generated rather than them needing to manually select it for each post. In the event that they’d later want to change the date of publication for a post for whatever reason, would the only way to do so be by changing the date in the folder name itself?

Nonetheless, I was curious about your suggestion of a date field and tried it out, but now the folders still generate with the date of publication prepended, rather than the “Published on” date I choose in the field. Does the date field not affect the folder naming? And in my corresponding php file, even when I change it to <$children = $page->children()->listed()->sortBy('date', 'desc');>, it still just shows the pages in the order they appear in my file system. sortBy('date', 'desc') and sortBy('date', 'asc') don’t seem to make a difference.

Here you’ll see that although I selected 02/06 as my “Published on” date, the folder still generates with 20250222.

If you call your field published, then you need to use that field name for the num option.

If you have such a date field, you can either prefill it at page creation, and/or use a hook to update it when the status is changed to public. And of course, the field can be edited manually.

I see, thank you! It works now. By prefill at page creation, do you mean manually prepending the date to the folder name, or some other method?

No, if you set the default: now,the current date will be filled in automatically at page creation.

Late reply, but thank you! I got it now.