Make WordPress Core


Ignore:
Timestamp:
06/09/2015 05:41:35 PM (10 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/meta/query.php

    r29964 r32713  
    807807        $this->assertNotContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
    808808    }
     809
     810    /**
     811     * @group 32592
     812     */
     813    public function test_has_or_relation_should_return_false() {
     814        $q = new WP_Meta_Query( array(
     815            'relation' => 'AND',
     816            array(
     817                'key' => 'foo',
     818                'value' => 'bar',
     819            ),
     820            array(
     821                'relation' => 'AND',
     822                array(
     823                    'key' => 'foo1',
     824                    'value' => 'bar',
     825                ),
     826                array(
     827                    'key' => 'foo2',
     828                    'value' => 'bar',
     829                ),
     830            ),
     831        ) );
     832
     833        $this->assertFalse( $q->has_or_relation() );
     834    }
     835
     836    /**
     837     * @group 32592
     838     */
     839    public function test_has_or_relation_should_return_true_for_top_level_or() {
     840        $q = new WP_Meta_Query( array(
     841            'relation' => 'OR',
     842            array(
     843                'key' => 'foo',
     844                'value' => 'bar',
     845            ),
     846            array(
     847                'relation' => 'AND',
     848                array(
     849                    'key' => 'foo1',
     850                    'value' => 'bar',
     851                ),
     852                array(
     853                    'key' => 'foo2',
     854                    'value' => 'bar',
     855                ),
     856            ),
     857        ) );
     858
     859        $this->assertTrue( $q->has_or_relation() );
     860    }
     861
     862    /**
     863     * @group 32592
     864     */
     865    public function test_has_or_relation_should_return_true_for_nested_or() {
     866        $q = new WP_Meta_Query( array(
     867            'relation' => 'AND',
     868            array(
     869                'key' => 'foo',
     870                'value' => 'bar',
     871            ),
     872            array(
     873                'relation' => 'OR',
     874                array(
     875                    'key' => 'foo1',
     876                    'value' => 'bar',
     877                ),
     878                array(
     879                    'key' => 'foo2',
     880                    'value' => 'bar',
     881                ),
     882            ),
     883        ) );
     884
     885        $this->assertTrue( $q->has_or_relation() );
     886    }
    809887}
Note: See TracChangeset for help on using the changeset viewer.