How to order blog posts by publish date with no date field

I created a blog that doesn’t display article dates, but I want the articles/posts to display with the most recent posts first. I’ve tried all sorts of ->sortBy() variants, including pubdate and publish_date with no success.

Probably not saying this right, but is there any way for me to call the creation/publish date without specifying a date at the time of creating the post? I feel like there is something very simple I’m missing.

Not sure this helps at all, but here is my base loop without any sort (I don’t specify visible because my blog posts are invisible…I don’t want them displaying in the navigation dropdown):

<?php $articles = $page->children()->flip()->paginate(10) ?>

<?php foreach($articles as $article): ?>

If you don’t specify a date when you create the page, then your only option is the modified() method.

<?php $articles = $page->children()->sortBy('modified', 'desc')->paginate(10) ?>

But note that the modified date changes whenever a page is, well, modified. Why don’t you want to use a date field?

Thank you. I originally designed it without the date field because I thought the posts would be “evergreen” and not produced very often, hence I didn’t want a date drawing attention to the fact the posts were old. Of course, I should have built it in anyway and just not displayed the date, esp considering now I want to both call the date and display it.

Is there a way to retro-fit the date into the existing posts? I tried various methods of adding the date with little success, even on newly created posts. I was able to add a date field via the post’s blueprint (using both date and datetime), thinking I could at least go back and manually assign dates to get the order to display correctly. But I was unable to get my sort function to work. I was also unable to get the date to display on either the main page or the post page. I found multiple ways other people accomplish this, but none of them seemed to work for me.

Here is what is currently on my main page. This displays all posts in what appears to be reverse alphabetical order according to the post’s title.

  template: newsarticle
  num: date
  field: date
  sort: flip

The setting you have in your blueprint is not a field, it only defines how subpages are numbered when you make them visible and how they are sorted. You have to add a date field to the newsarticle blueprint.

# ... some more fields
    label: Date
    type: date

I apologize, I should have also included my newsarticle blueprint, which already includes the date field exactly as you have it. I am able to add dates in the panel, I am just unable to call the date in order to sort OR to display despite various efforts based on research here and through others’ blog templates.

To sort your articles by the date field:

<?php $articles = $page->children()->sortBy('date', 'desc')->paginate(10) ?>

Is the field called “date”?

To output the date in the single article template:

<?= $page->date('Y-m-d') ?>

Or if you are in a loop:

<?php foreach($articles as $article): ?>
  <?= $article->date('Y-m-d') ?>
<?php endforeach ?>

Yes, the front end displays correctly now! I tried the ->sortBy('date', 'desc') previously, but maybe my inclusion of ->flip() , as well, may have been messing things up. Same thing with my main page/news blueprint file. Using sort: date desc instead of sort: flip fixed my display issue in the panel view.

As for displaying the date, that now works, as well. I just need to research/learn learn how to display the format differently. Thank you!

What format do you need?

Simply needed to look up what php date format to use to output a textual representation of the month and a single digit version of the date. I ended up with this version, and everything looks great.

<?= $page->date('F j,  Y') ?>