Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
notakoder opened this issue Jan 14, 2023 · 6 comments
Closed

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

notakoder opened this issue Jan 14, 2023 · 6 comments

Comments

@notakoder
Copy link

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
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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. 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants