Only display posts with comments

I ran into this same problem recently and developed a good solution that maintains paging (which some of the answers here do not) and also allows for only targeting certain comment types and comment approved statuses.

function has_comments_join( $join, &$wp_query ) {
    global $wpdb;
    if ($comment_term = $wp_query->get('post_has_comments')) {
        $join .= $wpdb->prepare(" JOIN $wpdb->comments ON (($wpdb->posts.ID = $wpdb->comments.comment_post_ID) AND ($wpdb->comments.comment_type = %s) AND ($wpdb->comments.comment_approved = %s)) ", $comment_term['comment_type'], $comment_term['comment_status']);
    }
    return $join;
}

$results = new WP_Query(array(
    ...
    'post_has_comments' => array(
        'comment_type' => 'review',
        'comment_status' => '1'
    )
);

This code can easily be refined to support multiple statuses, conditionals on type and status, etc… I even have a variant I use to do comparisons on comment meta, similar to the meta_query functionality.

Enjoy!

Leave a Comment