The comments_template_top_level_query_args WordPress PHP filter allows you to modify the arguments used in the top level comments query.
Usage
add_filter('comments_template_top_level_query_args', 'your_custom_function');
function your_custom_function($top_level_args) {
// your custom code here
return $top_level_args;
}
Parameters
$top_level_args(array): The top level query arguments for the comments template, which includes:count(bool): Whether to return a comment count.orderby(string|array): The field(s) to order by.post_id(int): The post ID.status(string|array): The comment status to limit results by.
More information
See WordPress Developer Resources: comments_template_top_level_query_args
Examples
Change orderby to ‘comment_date’
Modify the query to order comments by the comment date.
add_filter('comments_template_top_level_query_args', 'change_orderby_to_comment_date');
function change_orderby_to_comment_date($top_level_args) {
$top_level_args['orderby'] = 'comment_date';
return $top_level_args;
}
Order comments by author email
Order comments by the author’s email address.
add_filter('comments_template_top_level_query_args', 'order_by_author_email');
function order_by_author_email($top_level_args) {
$top_level_args['orderby'] = 'comment_author_email';
return $top_level_args;
}
Limit comments to approved status
Display only approved comments in the comments template.
add_filter('comments_template_top_level_query_args', 'limit_comments_to_approved_status');
function limit_comments_to_approved_status($top_level_args) {
$top_level_args['status'] = 'approve';
return $top_level_args;
}
Display comments count only
Show the total number of comments for a post without displaying the actual comments.
add_filter('comments_template_top_level_query_args', 'display_comments_count_only');
function display_comments_count_only($top_level_args) {
$top_level_args['count'] = true;
return $top_level_args;
}
Set comments per page
Set a custom number of comments to display per page.
add_filter('comments_template_top_level_query_args', 'set_comments_per_page');
function set_comments_per_page($top_level_args) {
$top_level_args['number'] = 10;
return $top_level_args;
}