Ordering nested comments by the latest reply
I’ve recently been working on a project that has pretty active comment threads. 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 (TC)
- each TC has its own multi-level replies (RC)
With out-the-box ordering, TC can only be sorted by oldest-to-newest or newest-to-oldest. We need TC to be sorted by RC’s timestamp.
So How Do We Sort Top-Level Comments By Their Replies?
It’s actually relatively simple:
- each TC gets a timestamp when it’s created – this is separate from its date because they have different uses.
- this timestamp is used as the
orderbyargument for the comments query.
- Then, whenever any TC or associated RCs receive a reply the TC’s timestamp is updated.
It’s that simple.
If you’d like to implement this clean and effective enhancement with your comments, please get in touch to find out more.