Using WordPress ‘comment_feed_where’ PHP filter

The comment_feed_where WordPress PHP filter allows you to modify the WHERE clause of the comments feed query before it is executed.

Usage

add_filter('comment_feed_where', 'your_custom_function', 10, 2);

function your_custom_function($cwhere, $query) {
    // Your custom code here

    return $cwhere;
}

Parameters

  • $cwhere (string): The WHERE clause of the query.
  • $query (WP_Query): The WP_Query instance (passed by reference).

More information

See WordPress Developer Resources: comment_feed_where

Examples

Exclude comments from a specific post

To exclude comments from a specific post in the comments feed, modify the WHERE clause:

add_filter('comment_feed_where', 'exclude_comments_from_specific_post', 10, 2);

function exclude_comments_from_specific_post($cwhere, $query) {
    global $wpdb;
    $post_id = 42; // Replace with the ID of the post you want to exclude comments from
    $cwhere .= " AND {$wpdb->comments}.comment_post_ID != $post_id";
    return $cwhere;
}

Show only comments from a specific category

To display only comments from posts belonging to a specific category:

add_filter('comment_feed_where', 'show_comments_from_specific_category', 10, 2);

function show_comments_from_specific_category($cwhere, $query) {
    global $wpdb;
    $category_id = 5; // Replace with your specific category ID
    $cwhere .= " AND {$wpdb->posts}.ID IN (
                  SELECT object_id FROM {$wpdb->term_relationships}
                  WHERE term_taxonomy_id = $category_id
                )";
    return $cwhere;
}