Make WordPress Core


Ignore:
Timestamp:
06/09/2015 05:41:35 PM (11 years ago)
Author:
boonebgorges
Message:

Avoid returning duplicate matches when using a meta query in WP_User_Query.

A meta_query containing an OR relation can result in the same record matching
multiple clauses, leading to duplicate results. The previous prevention against
duplicates [18178] #17582 became unreliable in 4.1 when WP_Meta_Query
introduced support for nested clauses. The current changeset adds a new method
WP_Meta_Query::has_or_relation() for checking whether an OR relation
appears anywhere in the query, and uses the new method in WP_User_Query to
enforce distinct results as necessary.

Props maxxsnake.
Fixes #32592.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/query.php

    r32683 r32713  
    790790        $this->assertEqualSets( $expected, $found );
    791791    }
     792
     793    /**
     794     * @ticket 32592
     795     */
     796    public function test_top_level_or_meta_query_should_eliminate_duplicate_matches() {
     797        $users = $this->factory->user->create_many( 3 );
     798
     799        add_user_meta( $users[0], 'foo', 'bar' );
     800        add_user_meta( $users[1], 'foo', 'bar' );
     801        add_user_meta( $users[0], 'foo2', 'bar2' );
     802
     803        $q = new WP_User_Query( array(
     804            'meta_query' => array(
     805                'relation' => 'OR',
     806                array(
     807                    'key' => 'foo',
     808                    'value' => 'bar',
     809                ),
     810                array(
     811                    'key' => 'foo2',
     812                    'value' => 'bar2',
     813                ),
     814            ),
     815        ) );
     816
     817        $found = wp_list_pluck( $q->get_results(), 'ID' );
     818        $expected = array( $users[0], $users[1] );
     819
     820        $this->assertEqualSets( $expected, $found );
     821    }
     822
     823    /**
     824     * @ticket 32592
     825     */
     826    public function test_nested_or_meta_query_should_eliminate_duplicate_matches() {
     827        $users = $this->factory->user->create_many( 3 );
     828
     829        add_user_meta( $users[0], 'foo', 'bar' );
     830        add_user_meta( $users[1], 'foo', 'bar' );
     831        add_user_meta( $users[0], 'foo2', 'bar2' );
     832        add_user_meta( $users[1], 'foo3', 'bar3' );
     833
     834        $q = new WP_User_Query( array(
     835            'meta_query' => array(
     836                'relation' => 'AND',
     837                array(
     838                    'key' => 'foo',
     839                    'value' => 'bar',
     840                ),
     841                array(
     842                    'relation' => 'OR',
     843                    array(
     844                        'key' => 'foo',
     845                        'value' => 'bar',
     846                    ),
     847                    array(
     848                        'key' => 'foo2',
     849                        'value' => 'bar2',
     850                    ),
     851                ),
     852            ),
     853        ) );
     854
     855        $found = wp_list_pluck( $q->get_results(), 'ID' );
     856        $expected = array( $users[0], $users[1] );
     857
     858        $this->assertEqualSets( $expected, $found );
     859    }
    792860}
Note: See TracChangeset for help on using the changeset viewer.