Using WordPress ‘comments_pre_query’ PHP filter

The comments_pre_query WordPress PHP filter allows you to modify the comments data before the query takes place.


add_filter('comments_pre_query', 'your_function_name', 10, 2);

function your_function_name($comment_data, $query) {
    // your custom code here

    return $comment_data;


  • $comment_data (array|int|null) – Return an array of comment data to short-circuit WP’s comment query, the comment count as an integer if $this->query_vars['count'] is set, or null to allow WP to run its normal queries.
  • $query (WP_Comment_Query) – The WP_Comment_Query instance, passed by reference.

More information

See WordPress Developer Resources: comments_pre_query


Modify comments based on a specific condition

In this example, we will remove comments containing a specific word.

add_filter('comments_pre_query', 'filter_comments_by_word', 10, 2);

function filter_comments_by_word($comment_data, $query) {
    $filtered_comments = array();

    if (is_array($comment_data)) {
        foreach ($comment_data as $comment) {
            if (strpos($comment->comment_content, 'specific_word') === false) {
                $filtered_comments[] = $comment;

    return $filtered_comments;