Make WordPress Core


Ignore:
Timestamp:
10/17/2014 01:57:18 AM (12 years ago)
Author:
boonebgorges
Message:

Comment/post author in/not_in for WP_Comment_Query.

Props nofearinc, chriscct7.
Fixes #29885.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r29808 r29935  
    258258         *
    259259         * @since 3.1.0
    260          * @since 4.1.0 Introduced 'comment__in', 'comment__not_in',
     260         * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
     261         *              'post_author__not_in', 'author__in', 'author__not_in',
    261262         *              'post__in', and 'post__not_in' to $query_vars.
    262263         *
     
    269270                $defaults = array(
    270271                        'author_email' => '',
     272                        'author__in' => '',
     273                        'author__not_in' => '',
    271274                        'fields' => '',
    272275                        'ID' => '',
     
    279282                        'order' => 'DESC',
    280283                        'parent' => '',
     284                        'post_author__in' => '',
     285                        'post_author__not_in' => '',
    281286                        'post_ID' => '',
    282287                        'post_id' => 0,
     
    468473                }
    469474
     475                // If any post-related query vars are passed, join the posts table.
     476                $join_posts_table = false;
    470477                $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
    471478                $post_fields = array_filter( $plucked );
    472479
    473480                if ( ! empty( $post_fields ) ) {
     481                        $join_posts_table = true;
     482                        foreach ( $post_fields as $field_name => $field_value ) {
     483                                $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
     484                        }
     485                }
     486
     487                // Comment author IDs for an IN clause.
     488                if ( ! empty( $this->query_vars['author__in'] ) ) {
     489                        $where .= ' AND user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
     490                }
     491
     492                // Comment author IDs for a NOT IN clause.
     493                if ( ! empty( $this->query_vars['author__not_in'] ) ) {
     494                        $where .= ' AND user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
     495                }
     496
     497                // Post author IDs for an IN clause.
     498                if ( ! empty( $this->query_vars['post_author__in'] ) ) {
     499                        $join_posts_table = true;
     500                        $where .= ' AND post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
     501                }
     502
     503                // Post author IDs for a NOT IN clause.
     504                if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
     505                        $join_posts_table = true;
     506                        $where .= ' AND post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
     507                }
     508
     509                if ( $join_posts_table ) {
    474510                        $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
    475                         foreach( $post_fields as $field_name => $field_value )
    476                                 $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$field_name} = %s", $field_value );
    477511                }
    478512
Note: See TracChangeset for help on using the changeset viewer.