Ordering blogposts by publishing date

Continuing the discussion from How can I get a blog to order post by creation date, rather than modification date?:

There is a plugin that auto dates every page created: https://github.com/getgrav/grav-plugin-auto-date

Is there another way, e.g. by adding a value in the item.md page?

Thanks!

@werdi, Did you have a look at the ordering options of Collections?

header.x The order is based on any page header field. i.e. header.taxonomy.year . Also a default can be added via a pipe. i.e. header.taxonomy.year|2015

To use the build-in publish_date, define the ordering of the collections as follows:

content:
  items: ...
  order:
    by: header.publish_date
    dir: asc

Variable publish_date is a standard field in the ā€˜Optionsā€™ tab for pages in Admin.
When creating a item.md, user can enter the ā€˜Publishe Dateā€™ field. It will create the following frontmatter in item.md.

publish_date: 01-01-2020

Hope this helpsā€¦

2 Likes

This looks good. So where would I enter this in order for it to affect all blogposts?

@werdi,

So where would I enter this in order for it to affect all blogposts?

What does this refer to? The definition of the ordering of the collection, or the ā€˜publishing_dateā€™?

@anon76427325 I meant, this bit you copied - I didnā€™t find it in thesystem.yaml - where does it go?

@werdi, You mention in your title ā€˜blogpostsā€™ so I guess that will be in the page blog.md where the collection of blog-items is defined.

The value for publish_date will go in each item.md

Thank you @anon76427325 it looks like it will work and Iā€™m in the middle of fixing it, since I had been using the Date field of the Options tab.

Now it seems that I have to change the date format to the US date, as grav now sorts the blog posts by day/month/year.

This is what I found on my system.yaml

dateformat:
    default: null
    short: 'jS M Y'
    long: 'F jS \a\t g:ia'

I cannot find any equivalent here to find my solution: https://learn.getgrav.org/16/content/headers#date

or here:
https://www.php.net/manual/en/datetime.formats.date.php

What am I missing?

@werdi, You didnā€™t mention the dateformat you would like to use, so I guess it will be the German format: dd.mm.yyyy.

In ā€˜sytem.yamlā€™ set pages.dateformat.default: 'd.m.Y'

In your question you mention ā€˜publish_dateā€™ while now you mention ā€˜dateā€™. You can use either of the two.

If you want to use the ā€˜Dateā€™ field in the header of the page, change the collection to:

---
date: 02.04.2020
content:
  items: ...
  order:
    by: date  # This is a build-in ordering field for collections
    dir: asc
---

I found a way in the configuration > system tab to set the date format - now I found out I do have to re-load each blog article for this to be actively working.

I like the European date format, but the ordering happens apparently like so: day-month-year, so a blog post published on 01-05-2016 would sort higher than a blogpost published on 02-01-2020. At least thatā€™s what it looked like to me.

@werdi,

I donā€™t think there is a European standard date format. Some use dd-mm-yyyy and others like Germany use dd.mm.yyyyā€™

The ISO standard is yyyy-mm-dd

I havenā€™t finalised debugging it, but it seems that if the ordering is using x.publish_date as I suggested, Grav doesnā€™t know it is a date field. Hence it will order it like a string as you noticed.

When ordering on ā€˜dateā€™ the ordering follows the internal value of the date.

@anon76427325 So if I understood you correctly, I would

  • move the value to the field ā€œdateā€ instead of the field ā€œpublish dateā€
  • change the ordering back to date on the blog.md

This would revert things back to where they were - the ordering was following the ā€œlast changedā€ timestamp. Which is not what I intended with my question.

With my question, I intend that a blogpost stays in the right order even if I modify it afterwards.

@werdi, According the docs:

The date variable allows you to specifically set a date associated with this page. This is often used to indicate when a post was created and can be used for display or sort-order purposes. If not set, this defaults to the last modified time of the page.

If the user explicitly sets the date I expect it to remain the same also after updating the page.

1 Like

@anon76427325 Me too, but it wasnā€™t happening on my blog. The person who initiated the discussion I was referring to in my OP had the same issue.

@werdi, I believe the OP of the other post, does not set the date value in Admin manually. In that case, Grav will use the default ā€˜modify dateā€™ as the date for that page. Therefor the ordering changes when editing a page.

Once set manually, it should remain the same despite any changes to the page.

@anon76427325 It makes total sense what you say, but this is not true for my blog. I tried setting up everything you suggested for the three last published blogposts. They donā€™t appear on top.

Apparently, re-saving a blog post without even modifying it brings it back up.

Blog posts which should appear up because of the date donā€™t.

I canā€™t find the system here.

@werdi, That is not my experienceā€¦

When I set the date in each page, via an editor or through the Admin plugin, their values do not change after saving the page later on.

The date is only cleared (and hence will have value ā€˜date-modifiedā€™) when the checkbox before the field in Admin is unchecked.

What are the values of date in the header of the page before and after saving the page?

@anon76427325 This is how it looks like

Either way, the checkbox is always checked.

Before and after saving the page it is the same date. Also, in either field. The date remains the same but the page jumps on top of the other blogposts.

These are the current settings in blog.md

content:
    order:
        by: date
        dir: desc

The three blogposts with the latest date (see the one in the screenshot, for example) donā€™t appear on top.

(And Iā€™ve kept the date format year-month-day for now)

Itā€™s possible that at this point the publish_date field is overriding the date field. Meaning, some blog posts I already set to publish date, some blog posts are still on date.

What I have found out is that

publish_date (when modified or re-saved without modifying)

trumps

date.

Even when the sorting is by date.

Thatā€™s why it makes no sense.

Likewise, if a blogpost where the value is set at date, re-saving makes no difference to the ordering.

@werdi, I cannot reproduce what you are seeing. And because I cannot see what is happening in you system, it is bit useless for me to continue diggingā€¦

At the moment I do not see any role of ā€˜publish_dateā€™ when sorting a collection on ā€˜dateā€™. Only whether ā€˜dateā€™ is being set or not.

Hope you find a working solution!