Using WordPress ‘found_comments_query’ PHP filter

The found_comments_query WordPress PHP filter allows you to modify the SQL query used to retrieve the found comment count.

Usage

add_filter( 'found_comments_query', 'your_custom_function', 10, 2 );
function your_custom_function( $found_comments_query, $comment_query ) {
    // your custom code here
    return $found_comments_query;
}

Parameters

  • $found_comments_query (string) – The SQL query string used for retrieving found comment count. Default is ‘SELECT FOUND_ROWS()’.
  • $comment_query (WP_Comment_Query) – The WP_Comment_Query instance related to the query.

More information

See WordPress Developer Resources: found_comments_query

Examples

Change the SQL query to count only approved comments

In this example, we modify the SQL query to count only approved comments.

add_filter( 'found_comments_query', 'count_approved_comments_only', 10, 2 );
function count_approved_comments_only( $found_comments_query, $comment_query ) {
    $found_comments_query = "SELECT COUNT(*) FROM {$comment_query->query_vars['wpdb']->comments} WHERE comment_approved = '1'";
    return $found_comments_query;
}

Exclude comments with a specific author email

In this example, we exclude comments with the author email ‘[email protected]‘ from the count.

add_filter( 'found_comments_query', 'exclude_specific_author_email', 10, 2 );
function exclude_specific_author_email( $found_comments_query, $comment_query ) {
    $found_comments_query = "SELECT COUNT(*) FROM {$comment_query->query_vars['wpdb']->comments} WHERE comment_author_email != '[email protected]'";
    return $found_comments_query;
}

Count comments only from a specific post type

In this example, we count comments only from the ‘product’ post type.

add_filter( 'found_comments_query', 'count_comments_from_specific_post_type', 10, 2 );
function count_comments_from_specific_post_type( $found_comments_query, $comment_query ) {
    $found_comments_query = "SELECT COUNT(*) FROM {$comment_query->query_vars['wpdb']->comments} INNER JOIN {$comment_query->query_vars['wpdb']->posts} ON {$comment_query->query_vars['wpdb']->comments}.comment_post_ID = {$comment_query->query_vars['wpdb']->posts}.ID WHERE {$comment_query->query_vars['wpdb']->posts}.post_type = 'product'";
    return $found_comments_query;
}

Count comments from a specific date range

In this example, we count comments only from a specific date range (e.g., between ‘2023-01-01’ and ‘2023-01-31’).

add_filter( 'found_comments_query', 'count_comments_from_date_range', 10, 2 );
function count_comments_from_date_range( $found_comments_query, $comment_query ) {
    $found_comments_query = "SELECT COUNT(*) FROM {$comment_query->query_vars['wpdb']->comments} WHERE comment_date >= '2023-01-01' AND comment_date <= '2023-01-31'";
    return $found_comments_query;
}

Count comments with a specific meta key

In this example, we count comments with a specific meta key called ‘featured_comment’.

add_filter( 'found_comments_query', 'count_comments_with_specific_meta_key', 10, 2 );
function count_comments_with_specific_meta_key( $found_comments_query,$comment_query ) {
$found_comments_query = "SELECT COUNT(*) FROM {$comment_query->query_vars['wpdb']->comments} INNER JOIN {$comment_query->query_vars['wpdb']->commentmeta} ON {$comment_query->query_vars['wpdb']->comments}.comment_ID = {$comment_query->query_vars['wpdb']->commentmeta}.comment_id WHERE {$comment_query->query_vars['wpdb']->commentmeta}.meta_key = 'featured_comment'";
return $found_comments_query;
}