question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blog posts page: custom sorting logic

See original GitHub issue

🚀 Feature

Let’s be less opinionated and let users decide on the sorting logic.

Have you read the Contributing Guidelines on issues?

Yes

Has this been requested on Canny?

Requested on Discord

Motivation

Sorting by descending time works in almost all cases, but we shouldn’t assume that all users use the blog for time-based information. E.g., sort by alphabetical order? Sort by tags?

API Design

A new comparePosts plugin option, which accepts a function that is passed to the sorting algorithm:

https://github.com/facebook/docusaurus/blob/b40e45943ec1a8d52d0f524983d38d9ba23ec6bc/packages/docusaurus-plugin-content-blog/src/blogUtils.ts#L345-L347

And yes, it will receive the entire blog post, with all of its metadata and content.

Have you tried building it?

No; should be trivial

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:20 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
slorbercommented, Dec 20, 2021

@factoidforrest

Well, we sometimes produce multiple blog posts per day, and it would be nice to individually control their position.

In this case, you can use the date frontmatter to specify a time: date: 2021-12-20T10:00:00Z

Or sometimes we have something REALLY important and we want to stick it to the top. We recently did the blog post about log4shell which is why this is coming up. lunasec.io/docs/blog/log4j-zero-day

Would you use position: 3 for such a post?

IMHO what we are looking for here is rather a way to “pin” a blog post at the top (similar to GitHub issues somehow).

It’s also a common pattern among bloggers to have featured posts, and drive more attention to specific articles:

We could allow a plugin option to feature a list of posts, put them at the top, and maybe also add them to the blog sidebar to make them accessible from all blog pages.

Using featured: true frontmatter can also be nice, but not sure how to ensure an order in this case though


Using something like position: number is quite ambiguous, particularly when not all items have an explicit position, and we can’t prevent users to use the same position twice. + when adding a new blog post, do you really want to edit and offset all previously defined positions 😅

If this position is only applied inside a single day, it would be a quite unintuitive option, and using an hour in frontmatter can be enough.

1reaction
cerkiewnycommented, Oct 27, 2021

I don’t think it is quite as easy to add as another plugin, you have the contextual “next” and “previous” generation for the blog posts, which would be really awkward if they were sorted differently to the plugin sorting. I don’t have strong feelings about this feature, and I would personally add something looking more like enum:

{
  blogSorting: chronologicalDesc // also would have few more options like chronologicalAsc, maybe alphabeticalDesc/alphabeticalAsc
}

My feeling is that the only strong use case here is reversing the order from oldest to newest

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Arrange WordPress Posts in Any Order - WPMU Dev
Implement the custom sort order by modifying the main WordPress loop or building a custom loop and adding it to a sidebar widget...
Read more >
How to sort blog posts by a custom field - HubSpot Community
We ran into this issue where we wanted to sort items from a blog based on the contents of a custom field/module we...
Read more >
3 Ways to Re-Order WordPress Blog Posts - ThemeIsle
We cover three easy methods to re-order WordPress blog posts on the front-end. Step by step. Can be done on any blog or...
Read more >
4 Easy Ways to Re Order Blog Posts in WordPress - YouTube
Do you want to change the order of your blog posts in WordPress? By default, WordPress shows posts in reverse chronological order where ......
Read more >
5 Best Ways to Re-Order Posts in WordPress - Rank Math
You may need to rearrange a custom post type or want a WooCommerce product ... on your website may not always be to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found