Using WordPress ‘get_page_of_comment_query_args’ PHP filter

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

Usage

add_filter('get_page_of_comment_query_args', 'your_custom_function', 10, 1);

function your_custom_function($args) {
    // your custom code here
    return $args;
}

Parameters

  • $args (array): Array of WP_Comment_Query arguments, which include:
    • type (string): Limit paginated comments to a given type. Accepts ‘comment’, ‘trackback’, ‘pingback’, ‘pings’ (trackbacks and pingbacks), or ‘all’. Default ‘all’.
    • post_id (int): ID of the post.
    • fields (string): Comment fields to return.
    • count (bool): Whether to return a comment count (true) or array of comment objects (false).
    • status (string): Comment status.
    • parent (int): Parent ID of the comment to retrieve children of.
    • date_query (array): Date query clauses to limit comments by. See WP_Date_Query.
    • include_unapproved (array): Array of IDs or email addresses whose unapproved comments will be included in paginated comments.

More information

See WordPress Developer Resources: get_page_of_comment_query_args

Examples

Change comment type to ‘comment’

Limit comments to only ‘comment’ type, excluding ‘trackback’, ‘pingback’, and ‘pings’.

add_filter('get_page_of_comment_query_args', 'change_comment_type', 10, 1);

function change_comment_type($args) {
    $args['type'] = 'comment';
    return $args;
}

Filter comments by post ID

Show comments only for a specific post ID.

add_filter('get_page_of_comment_query_args', 'filter_by_post_id', 10, 1);

function filter_by_post_id($args) {
    $args['post_id'] = 123; // Change 123 to the desired post ID
    return $args;
}

Return only approved comments

Only return comments with an ‘approved’ status.

add_filter('get_page_of_comment_query_args', 'only_approved_comments', 10, 1);

function only_approved_comments($args) {
    $args['status'] = 'approve';
    return $args;
}

Exclude comments with a specific parent ID

Exclude comments that have a specific parent ID.

add_filter('get_page_of_comment_query_args', 'exclude_parent_comments', 10, 1);

function exclude_parent_comments($args) {
    $args['parent__not_in'] = array(456); // Change 456 to the parent ID to exclude
    return $args;
}

Include comments from specific email addresses

Include unapproved comments from specific email addresses.

add_filter('get_page_of_comment_query_args', 'include_specific_email_comments', 10, 1);

function include_specific_email_comments($args) {
    $args['include_unapproved'] = array('[email protected]', '[email protected]');
    return $args;
}