How can I get a blog to order post by creation date, rather than modification date?

New Grav/Twig/etc. user here. Working my way through setting up a blog, showing news articles. All going well except, if say I have three articles - One, Two and Three in that order, then I go and edit number two, the ordering changes to Two, One Three… I want them to be arranged by the date they were created, not last modified.

My frontmatter for the parent ‘news’ page is as follows:

title: News
content:
items: '@self.children'
limit: '10'
order:
    by: date
    dir: desc
pagination: '1'

But I cannot, for the life of me work out how to achieve what I’m sure is a very simple thing… Any help would be really appreciated!

first, welcome to grav, as always for new posters :smile:
then, as a proposal: why not just reset the date in the frontmatter of your post to the initial value ?

Hello and thank you for the welcome… :slightly_smiling_face:

Once built, I’m handing this website back to a client who really won’t be suited to doing things like that. They’ll forget to do it every time and basically will think that the ordering system is broken.

I’m surprised that ordering by date doesn’t seem to take into post creation date, modification date or even publish date… (Or at least I’ve not been able to work it out) Surely there’s a way to do stuff like this?

While getting used to Grav, I’ve been surprised at how powerful it appears to be. I’ve used many CMS in the past and don’t seem to remember having run into this sort of problem before… Or am I missing your point?

Thanks for your help so far though!

Ok, I see your issue :slight_smile:
Unfortunately, I have no solution at hand, as I did not look into this so far, I`m using grav mostly headless, that means i do not edit posts or pages in the admin, but in my local IDE/editor and then sync to the webspace.
Maybe soemone else can help here, your last option,however, would be to change some code in the grav admin code.
But I suppose there is still an easier way.

1 Like

Just been through the documentation and can’t find a creation-date variable. However, if you made a field called something like ‘article_number’ you could sort by that field; assuming your customer is using the Admin panel to create posts, you can tell them to remember to fill out the field. Although it’s more hassle for them, and has the risk of them doing it wrong, it has a plus side, in that if they want to to modify the order of posts they can do so. So for example they might post about “My trip to France” and then realise that they should first have posted about “My trip to Germany”, because they did that first. All they need to do is to modify the article_number variable.

That’s all that I can suggest. If an automatic creation-date variable turns up I too would be pleased to know, so thanks for asking the question.

1 Like

Firstly, thanks to all who tried to help! I thought I’d update everyone…

Actually, I forgot to mention why this was important to me and what I was trying to achieve. I understood that I’d be able to rearrange the order of posts easily - without having to resort to dates if I enabled ‘Folder Numeric Prefix’ - The thing was that my theme which was based on Bootstrap4 then showed these child pages within a dropdown*. I wanted a single navigation link that landed on a page that then showed blog post summaries, linking through to blog posts (child pages) in a defined order - which was creation date, rather than modification date.

(*Actually, I had to enable dropdowns for navigation elements in the theme configuration, because I needed more than one.)

To ‘solve’ my issue I finally had to cheat a bit… I manually added a static link into navigation which linked to a new parent page that had the child pages I needed and this parent page had ‘Folder Numeric Prefixes’ - So each of the child pages could be ordered as desired.

The down-side to this is that the manually added link has to be after the dynamic navigation - so always the last link in the navigation, but until I can figure out a way to alleviate that, this will have to do.

I attempted to get the user to define a ‘published on’ date when creating the blog posts, using a blueprint to store the value in frontmatter, but had no luck in being able to sort by that value anyway… I’m retrieving values from frontmatter for use by twig in other places - it just seemed to be unhappy to sort by dateTime values for me.

This is really just to record the things I ran into issues with, and how I managed to side-step around them, in case anyone else finds themselves stumped trying to do something similar in the future. I’ve read so many posts here where users ask something and then don’t report back, which really isn’t that helpful. I mean, did they find an answer, give up, are they still trying… etc? I would have asked, but lots of posts are several years old, so I assume they’d either worked it out or given up.

Anyway,… There you go.

I can’t verify this plugin works, but it looks to have potential to do exactly what you want (hopefully!). https://github.com/getgrav/grav-plugin-auto-date

I’d love to know if you have luck with it though because this kind of functionality is for sure very necessary in many cases.

Thanks @onetrev! I’ll have a look at that…

I’m not too sure whether I’d be able to get the ordering to work on a date in Frontmatter though, I had no luck when requiring the post to have a ‘created on’ date in the blueprint, but as this plugin exists, it at least gives me hope that it might be possible.

Thanks once again!