How to order the Replies of the Comments

WordPress has a good interface for ordering and showing the comments of a post for the themes. You simply have to do click on the Admin’s menu Settings / Discussion and in the section Other comment settings, the last two lines offers you three fields for controlling it: number of comments for each page, which page is shown in first position (the last or the first) and, of course, the order of the comments (older or newer comments) for each comments page… But, what about the replies of each individual comment? How to control if these replies are displayed in ascending or descending order?

How to order the Replies of the Comments

Though in terms of data type, comments and replies are the same, due to their hierarchical structure, WordPress makes a separated treatment for the comments and for the replies to each comment therefore it makes two separated queries, one for each kind of data.

The first query fetches the top level comments of a post. This is the query controled by the parameters included in the Settings/Discussion page but the second query (set of queries) –what fetches the replies for each individual comment–, it is not controled by these parameters but rather, if you want to change the order of this second query, you have to edit your comments.php template for introducing an additional parameter in the set of arguments that is received by the function commonly used for showing the comments, ie, wp_list_comments.

The function wp_list_comments is a complex utility that allows the users to show the post comments and their replies in many different ways so that this function has a lot of parameters however, now, we only are interested in one of them, specificaly, the parameter reverse_children. Changing this boolean parameter from TRUE to FALSE and vice versa, we’ll control how the replies are ordered, and shown on the screen.

$args = array(
   // arguments ...
   'reverse_children' => TRUE,
   // more arguments ...
wp_list_comments( $args );

So, as in the former example, if you want to show the replies of to the comments in a different way, open the comments.php file of your theme, locate the function wp_list_comments and check what set of arguments is sent to this function… And just add to the set of arguments, the new parameter reverse_children. Set this parameter to TRUE if you want to see the comments with the newest in the first position or set the parameter to FALSE (or do nothing, because this is the default option of WordPress) for the contrary option.

Have a nice WordPressing!

Leave a Comment

   Mandatory field
You can use these HTML tags inside the commment.
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>