picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.81k stars 616 forks source link

Sorting pages using a customer header isn't honoured. #657

Closed notakoder closed 1 year ago

notakoder commented 1 year ago

I am sorting pages (posts) per a meta header.

<ul>
{% if (current_page.id == "categories") %}
{% for page in pages|sort_by("page.meta.Catposition") %}
    {% if (page.meta.Category == "Sections") and not page.hidden %}
        <li><a href="{{ page.url }}">{{ page.title }}</a></li>
    {% endif %}
{% endfor %}
{% endif %}
</ul>
---
Title: Title of the page
Template: template_name
Position: 1
Category: Sections
Catposition: 1
---

Earlier, the sorting was done by Position and it worked. I later changed it to Catposition and added this header to all the required pages numbered them correctly. But for some reason, sorting by Catposition just does not work. Posts are listed alphabetically. However, the moment I revert the sorting code to page.meta.Position, lists are sorted as per the Position. It is as if something is syntactically wrong with the code containing Catposition even though it is an exact copy with a change in the header. Any idea what could be wrong?

mayamcdougall commented 1 year ago

I'm not seeing anything obvious here... if it works with Position, and you've change all occurrences to Catposition instead, it should function exactly the same. The fact that it's falling back to alphabetical sort would probably imply that it's not finding Catposition in the page.

I know it's not helpful, but maybe double check everything for typos? Copy and paste Catposition between your code and your metadata just to sanity-check that it matches, etc.

@PhrozenByte Do you have any thoughts on this?

PhrozenByte commented 1 year ago

Check for typos and upper/lowercase (especially when the meta header was registered using a plugin's onMetaHeaders or the theme's pico-theme.yml)

notakoder commented 1 year ago

I figured out what's causing it. For next and previous buttons I had added a sorting configuration in the config.yml as per this discussion.

sort_directories:
    - docs
pages_order_by: meta
pages_order_by_meta: position
pages_order: asc

This is overriding my {% for page in pages|sort_by("page.meta.Number")%} in the .twig template. If I change pages_order_by_meta: position above to pages_order_by_meta: Catposition, the order of posts listings is correct. But I can't change that since elsewhere in the website, I need the pages to be sorted as per position. Don't you think that the {% for page in pages|sort_by("page.meta.Catposition")%} in the template must have overridden the sorting order in config file since templates are the bare metal layer to listings' page?

mayamcdougall commented 1 year ago

To be honest, what it sounds like is that your original code just didn't work. It only appeared to work because you were already sorting pages by position globally.

And, I'm realizing now that I'm looking at the docs, that your syntax for sort_by is wrong. 😅

It should be:

{% for page in pages|sort_by(['meta', 'Number'])%}

Not:

{% for page in pages|sort_by("page.meta.Number")%}

So, why don't you give that a try and see if it behaves right. 😉

notakoder commented 1 year ago

So, why don't you give that a try and see if it behaves right.

It does. Crazy! I don't know how I got the idea of using page.meta.name. Perhaps it was an edit to the listing code that sorted pages as per default headers (title, time, etc).

Thanks for the help.

mayamcdougall commented 1 year ago

No worries. It happens. 😉

It is an odd syntax. On the technical side, this is because you're giving a Pico function some strings as arguments rather than reading them inside of Twig.

Don't feel too bad though, me and @PhrozenByte didn't catch that one either. 😅