Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Collection ordering doesnt work

Solved by pamtbaau View solution

Started by Danilo Puchelt 4 years ago · 6 replies · 605 views
4 years ago

I'm trying to order my news by publish_date. This is what I have so far:

TWIG
{% set collection = grav.page.collection({
    'items': {
        '@taxonomy': {
            'category': 'Blog',
            'tag': 'Spielbericht'
        }
    },
    'order': {
        'by': 'header.publish_date',
        'dir': 'asc'
    },
    'limit': 6,
    'published': true
}) %}

The result is:

TXT
11.05.2022
04.05.2022
09.05.2022
09.05.2022
09.05.2022
09.05.2022

Apparently nothing is sorted here. What am i doing wrong?

👍 1
4 years ago

Interesting. I'd start by looking at your date format settings, although it looks like whichever format is being used (sensible or US ridiculous :P), they are out of order.

Could you please post here those settings, as well as how you are outputting your collection?

Also, is publish_date explicitly set on all of these pages? I ask because some of the fallbacks for dates are not intuitive in my opinion.

👍 1
4 years ago

I've controlled all pages for publish_date and the date value and it's set for every single page. This is how a news page looks like:

YAML
---
title: 'Spielbericht #12'
firstTeamScore: 0
secondTeamScore: 0
taxonomy:
    category:
        - Blog
    tag:
        - Spielbericht
date: '05-11-2021 00:00'
publish_date: '05-11-2021 00:00'
---

This is the markup for my debug ouput:

TWIG
<div class="mt-5 pt-5">
    {% for page in collection %}
        {{  page.header.publish_date|date('d.m.Y') }} - {{  page.date|date('d.m.Y') }} - {{  page.header.title|raw }}<br>
    {% endfor %}
</div>

And here the date configuration from user/config/system.yaml:

YAML
  dateformat:
    default:                                     # The default date format Grav expects in the `date: ` field
    short: 'jS M Y'                              # Short date format
    long: 'F jS \a\t g:ia'                       # Long date format
    publish_dates: true  
last edited 05/13/22 by Danilo Puchelt
4 years ago

I had a quick look into the Grav core code and found a resolved issue that gave me some clues. I still don't fully understand how it is sorting this because even as a string, it's not in order.

Try these:

  • In your collection definition, change order.by to just "publish_date". It's not a documented option but does appear to be supported, and it sorts differently in the code.
  • Try the sort_flags option documented there, as it appears to be involved in the issue mentioned. No guidance given and not sure what value you would use in Twig, but try the ones linked like 'SORT_REGULAR' (as a string I guess??) etc. So:
TWIG
'order': {
        'by': 'header.publish_date',
        'dir': 'asc',
        'sort_flags': 'SORT_.........'
    },

Do you know much about debugging in Grav? By selectively dumping values into the debug console, you might be able to see where the problem is introduced.

Otherwise, you might get more satisfaction from someone on Discord. If you do, mention that you posted here and link to this, and also report back here what you find out. It should be valid to ask a question in a Github issue too (this might even be a bug), but it's really a question of luck whether they get looked at or answered.

4 years ago

Hello @hughbris, thanks for effort to find a solution. I've tried using the sort_flags parameter without success. I think sorting a collection should not be that complex. Due to time constraints I will use the chronological sorting with date now.

4 years ago

@hughbris:
'by': 'header.publish_date',

Try changing this to 'by': 'publish_date', though, it's quite simple and might work.

👍 1
4 years ago Solution

@hughbris, I think you are right...

Without using "header.", the publish_date is treated as a date. When using header.publish_date the value seems to be treated as a string.

Here are some ordering results:

TXT
By: header.publish_date, asc

01:01 02/02/2022
02:02 01/01/2022

The above result matches a string sort and not a date sort.

TXT
By: publish_date, asc

02:02 01/01/2022
01:01 02/02/2022

This is a correct sorting by date.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 54 11 hours ago
Support · by Anna, 3 days ago
2 60 14 hours ago
Support · by Justin Young, 15 hours ago
1 30 15 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 57 5 days ago