Show only posts which can be commented

Because comment_status is not a built-in query argument – but you can easily implement it:

function wpse_214323_query_by_comment_status( $where, $wp_query ) {
    global $wpdb;

    if ( ! empty( $wp_query->query_vars['comment_status'] ) ) {
        $status = $wp_query->query_vars['comment_status'];

        if ( $status !== 'open' )
            $status="closed";

        $where .= " AND $wpdb->posts.comment_status="$status"";
    }

    return $where;
}

add_filter( 'posts_where', 'wpse_214323_query_by_comment_status', 10, 2 );

Use this in addition to your existing code, now you can query 'comment_status' => 'open/closed'

Leave a Comment