Using WordPress ‘comments_template_query_args’ PHP filter

The comments_template_query_args WordPress PHP filter allows you to modify the arguments used to query comments in the comments_template() function.

Usage

add_filter('comments_template_query_args', 'my_custom_comments_args');
function my_custom_comments_args($comment_args) {
    // Your custom code here

    return $comment_args;
}

Parameters

  • $comment_args (array): Array of WP_Comment_Query arguments, including:
    • orderby (string|array): Field(s) to order by.
    • order (string): Order of results. Accepts ‘ASC’ or ‘DESC’.
    • status (string): Comment status.
    • include_unapproved (array): Array of IDs or email addresses whose unapproved comments will be included in results.
    • post_id (int): ID of the post.
    • no_found_rows (bool): Whether to refrain from querying for found rows.
    • update_comment_meta_cache (bool): Whether to prime cache for comment meta.
    • hierarchical (bool|string): Whether to query for comments hierarchically.
    • offset (int): Comment offset.
    • number (int): Number of comments to fetch.

More information

See WordPress Developer Resources: comments_template_query_args

Examples

Change comment order to ascending

To change the order of comments to ascending by comment date, you can modify the order argument:

add_filter('comments_template_query_args', 'change_comment_order');
function change_comment_order($comment_args) {
    $comment_args['order'] = 'ASC';
    return $comment_args;
}

Limit the number of comments displayed

To limit the number of comments displayed to a specific number, modify the number argument:

add_filter('comments_template_query_args', 'limit_comments_number');
function limit_comments_number($comment_args) {
    $comment_args['number'] = 5; // Display only 5 comments
    return $comment_args;
}

Exclude specific comments by status

To exclude specific comments based on their status, update the status argument:

add_filter('comments_template_query_args', 'exclude_comments_by_status');
function exclude_comments_by_status($comment_args) {
    $comment_args['status'] = 'approve'; // Show only approved comments
    return $comment_args;
}

Include unapproved comments for specific users

To include unapproved comments for specific users, modify the include_unapproved argument:

add_filter('comments_template_query_args', 'include_unapproved_comments');
function include_unapproved_comments($comment_args) {
    $comment_args['include_unapproved'] = array(1, 2, 3); // Include unapproved comments for users with IDs 1, 2, and 3
    return $comment_args;
}

Order comments by a custom meta field

To order comments by a custom meta field, update the orderby and meta_key arguments:

add_filter('comments_template_query_args', 'order_comments_by_custom_field');
function order_comments_by_custom_field($comment_args) {
    $comment_args['orderby'] = 'meta_value';
    $comment_args['meta_key'] = 'custom_field_key'; // Replace with your custom field key
    return $comment_args;
}