WordPress.org

Make WordPress Core

Ticket #12668: 12668-comments-query.patch

File 12668-comments-query.patch, 1.7 KB (added by mordauk, 4 years ago)
  • src/wp-includes/comment.php

     
    131131 * @uses $wpdb
    132132 *
    133133 * @param int $post_id The ID of the post
     134 * @param string $comment_type The type of comment to get
    134135 * @return array $comments The approved comments
    135136 */
    136 function get_approved_comments($post_id) {
     137function get_approved_comments( $post_id, $comment_type = '' ) {
    137138        global $wpdb;
    138         return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
     139
     140        $type = "";
     141        if( ! empty( $comment_type ) ) {
     142                $type = " AND comment_type = '%s'";
     143        }
     144
     145        return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'$type ORDER BY comment_date", $post_id, $comment_type ) );
    139146}
    140147
    141148/**
     
    447454                } elseif( 'pings' == $this->query_vars['type'] ) {
    448455                        $where .= ' AND comment_type IN ("pingback", "trackback")';
    449456                } elseif ( ! empty( $this->query_vars['type'] ) ) {
    450                         $where .= $wpdb->prepare( ' AND comment_type = %s', $this->query_vars['type'] );
     457
     458                        if( is_array( $this->query_vars['type'] ) ) {
     459
     460                                if( in_array( 'comment', $this->query_vars['type'] ) ) {
     461                                        // Default comments don't have an explcit type
     462                                        $this->query_vars['type'][] = '';
     463                                }
     464
     465                                $where .= ' AND comment_type IN ("' . implode( '","', $this->query_vars['type'] ) . '")';
     466
     467                        } else {
     468
     469                                $where .= $wpdb->prepare( ' AND comment_type = %s', $this->query_vars['type'] );
     470                       
     471                        }
    451472                }
    452473
    453474                if ( '' !== $this->query_vars['parent'] ) {