How can I sort the topic replies by date

Hello. I don’t know if it’s possible, and if it is I googled a lot and did not find anything about this, but I would like to sort all the replies of all the topics of my forum by the created date.

I’ve been working on a migration of data from an old forum to a brand new Discourse one. To do that, I am recreating the topics and replies via API so that I can send them to Discourse on the date the reply was created initially. That has been working just fine as when I go to the created replies on Discourse I can see that the original date is over there.

But even though all the replies are with the original date, they are sorted by the order they were created on the API. I don’t know if this is a bug or meant to be or if there is any way to sort them by the date I set on the API POST call, but if so I’d like some help on this topic. Cheers guys.

I am using version 2.8.8.

Don’t do that. You want to use an import script. You can look at discourse/script/import_scripts at main · discourse/discourse · GitHub for examples and search here for instructions for running import scripts for other forums.

Posts are displayed in the order in which they are created. So you’d need to have your API script (that you really don’t want to use) create them in the proper order.

1 Like

Hmmm, thanks for the reply. I did think about using a script a while ago but since Discourse has a lot of table relationships I decided to go through the API. But is there any way to create a script that will insert data in one table and then Discourse handles the rest of the relationship? That might make things easier for me.

But I mean, the migration via API has been working so far. I wonder what is the point of being able to send a created_at attribute on the endpoint to create posts if Discourse won’t show it sorted by it anyway… Shouldn’t this be a feature request?

Yes, in fact, there is! I linked to examples.

If that were true, you’d not have posted. :wink:

No, because the way to solve your problem is to use one of the dozens of examples that I linked to is the way to solve your problem. I promise it will be much, much easier. I wrote several import scripts before I really even learned ruby.

Even if you wanted to use the API, you still need to create the posts in the created_at order. There are ways to sort them in a plugin v(or maybe theme component?) , but you really don’t want to do that.

@JammyDodger points out that there is a rake task

    rake posts:reorder_posts

That might solve your problem. If you are importing a few dozens of topics and a few hundreds of posts, you might give that a try.


I see. I will keep following my idea, just wanted to know regarding the created_at attribute on the POST call when creating a post - which is, as it seems, useless, if the posts can’t be sorted by it. Thanks anyway.

Just FYI, the reason things don’t just sort by created date is because things are designed to support moving posts from one topic to another, and in that scenario, it’s proven less desireable to have posts get “mixed up” if the dates overlap.

Here’s an earlier discussion about that:

On the topic of how best to approach a migration, I agree w/ Jay. You’re likely better off following prior art here. While not a web API, the existing scripts rely on a lower level “migration API” of sorts with this in mind.

The web API is largely designed as an API for the front-end Discourse app. While it’s successfully used for various integrations, it is optimized for it’s primary purpose.


It’s not useless, it sets the created_at date as advertised. Just sort your posts by created_at before you create them.