Make WordPress Core


Ignore:
Timestamp:
06/09/2015 05:41:35 PM (9 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/src/wp-includes/meta.php

    r32610 r32713  
    955955
    956956    /**
     957     * Whether the query contains any OR relations.
     958     *
     959     * @since 4.3.0
     960     * @access protected
     961     * @var bool
     962     */
     963    protected $has_or_relation = false;
     964
     965    /**
    957966     * Constructor.
    958967     *
     
    10471056        if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
    10481057            $clean_queries['relation'] = 'OR';
     1058            $this->has_or_relation = true;
    10491059
    10501060        /*
     
    15791589        return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ) ;
    15801590    }
     1591
     1592    /**
     1593     * Check whether the current query has any OR relations.
     1594     *
     1595     * In some cases, the presence of an OR relation somewhere in the query will require the use of a DISTINCT or
     1596     * GROUP BY keyword in the SELECT clause. The current method can be used in these cases to determine whether
     1597     * such a clause is necessary.
     1598     *
     1599     * @since 4.3.0
     1600     *
     1601     * @return bool True if the query contains any OR relations, otherwise false.
     1602     */
     1603    public function has_or_relation() {
     1604        return $this->has_or_relation;
     1605    }
    15811606}
    15821607
Note: See TracChangeset for help on using the changeset viewer.