[wp-trac] [WordPress Trac] #32592: Duplicate user results when using nested meta queries with inner OR relation
WordPress Trac
noreply at wordpress.org
Tue Jun 9 16:35:02 UTC 2015
#32592: Duplicate user results when using nested meta queries with inner OR
relation
--------------------------+--------------------
Reporter: maxxsnake | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: 4.3
Component: Users | Version: 4.2.2
Severity: normal | Resolution:
Keywords: has-patch | Focuses:
--------------------------+--------------------
Comment (by maxxsnake):
Hi Boone
Looks like it should do the trick, thanks for the quick response. I was
using a slightly messier approach to resolve the matter for my current
project, although without modifying core directly I believe it was as best
as I could have implemented. Will be awaiting it's removal once the patch
is available :-)
My temporary hack was to recursively search the meta query for an OR
relation:
{{{
<?php
class Project_Class {
public static function select_distinct_users( $query ) {
if ( ! empty( $query->meta_query->queries ) &&
self::_has_nested_relation( 'OR', $query->meta_query->queries ) ) {
if ( ! empty( $query->query_fields ) && strpos(
$query->query_fields, 'DISTINCT' ) !== 0 ) {
$query->query_fields = 'DISTINCT ' .
$query->query_fields;
}
}
}
private static function _has_nested_relation( $relation, $queries
) {
if ( is_array( $queries ) ) {
foreach ( $queries as $key => $value ) {
if ( is_array( $value ) ) {
$value =
self::_has_nested_relation( $relation, $value );
if ( $value !== false ) {
return true;
}
}
elseif ( $key === 'relation' && $value ===
$relation ) {
return true;
}
}
return false;
}
}
}
add_filter( 'pre_user_query', array( 'Project_Class',
'select_distinct_users' ) ); // In actual project added filter only where
necessary
?>
}}}
Naturally, since recursion is already occurring inside
WP_Meta_Query->sanitize_query(), it makes more sense in core to go ahead
and set the property to trigger DISTINCT at that time as you have already
done.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/32592#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list