Make WordPress Core

Changeset 36326


Ignore:
Timestamp:
01/15/2016 08:09:36 PM (9 years ago)
Author:
boonebgorges
Message:

Respect all post-related filters in WP_Comment_Query.

The refactor of WP_Comment_Query's SQL generation in [34542] introduced a bug
that caused only the last post-related filter to be respected in comment
queries. In other words, if querying for comments using params
post_status=draft&post_author=3, only the last-processed of these params
would be respected. The current changeset fixes the logic so that these clauses
don't overwrite each other.

Props chriscct7.
Fixes #35478.

Location:
trunk
Files:
2 edited

Legend:

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

    r36277 r36326  
    766766                // $field_value may be an array.
    767767                $esses = array_fill( 0, count( (array) $field_value ), '%s' );
    768                 $this->sql_clauses['where']['post_fields'] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
     768                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
    769769            }
    770770        }
  • trunk/tests/phpunit/tests/comment/query.php

    r36279 r36326  
    533533
    534534        $this->assertEqualSets( array( $c1, $c2 ), $found );
     535    }
     536
     537    /**
     538     * @ticket 35478
     539     */
     540    public function test_multiple_post_fields_should_all_be_respected() {
     541        $posts = array();
     542
     543        $posts[] = self::factory()->post->create( array(
     544            'post_status' => 'publish',
     545            'post_author' => 3,
     546        ) );
     547
     548        $posts[] = self::factory()->post->create( array(
     549            'post_status' => 'draft',
     550            'post_author' => 4,
     551        ) );
     552
     553        $posts[] = self::factory()->post->create( array(
     554            'post_status' => 'draft',
     555            'post_author' => 3,
     556        ) );
     557
     558        $comments = array();
     559        foreach ( $posts as $post ) {
     560            $comments[] = self::factory()->comment->create( array(
     561                'comment_post_ID' => $post,
     562            ) );
     563        }
     564
     565        $q = new WP_Comment_Query( array(
     566            'post_status' => 'draft',
     567            'post_author' => 3,
     568            'fields' => 'ids',
     569        ) );
     570
     571        $this->assertSame( array( $comments[2] ), $q->comments );
    535572    }
    536573
Note: See TracChangeset for help on using the changeset viewer.