Ordering nested comments by the latest reply
I’ve recently been working on a project that has pretty active comment threads and needed to control the ordering of nested comments. To improve the user experience we decided it would be much clearer to show the newest activity at the top of the comments.
Now, you may be thinking that WordPress has a setting for that?
It does not.
Via WordPress, you can order the top-level comments, but not the nested replies.
Nest Comments Scenario:
- 10 top-level comments.
- Each top-level comment has its own multi-level replies.
With out-the-box ordering, top-level comments can only be sorted by oldest-to-newest or newest-to-oldest. We need them to be sorted by the multi-level replies’ timestamp.
So How Do We Order Nested Comments to Sort Top-Level Comments By Their Replies?
It’s actually relatively simple:
- Each top-level comment gets a timestamp when it’s created – this is separate from its date because they have different uses.
- This timestamp is used as the
orderby
argument for the comments query. - Then, whenever any top-level comment or associated reply receives a reply the top-level comment timestamp is updated.
It’s that simple.
Ordering nested comments in this way is a nice way to keep active threads at the top of the comment list. If you’d like to implement this clean and effective enhancement for your comments, please get in touch to find out more.