User appears twice in a WP_User_Query

Try this for kicks…

Solution 1

Note: Does not work with WP_User_Query (?)

add_filter('posts_distinct', 'user_meta_query_distinct');

//your query here...

remove_filter('posts_distinct', 'user_meta_query_distinct');

function user_meta_query_distinct() { 
    return "DISTINCT"; 
}

Solution 2

Can you try adding 'relation' => 'OR' to your meta_query:

$user_query = new WP_User_Query( 
    array(
        'role'       => 'member',
        'orderby'    => 'registered',
        'order'      => 'DESC',
        'meta_query' => array(
             'relation' => 'OR', //should result in a DISTINCT query
             array(
                 'key'   => 'onTrial',
                 'value' => '1',
             ),
        ),
    )
);

See this diff:

https://core.trac.wordpress.org/attachment/ticket/17582/17582.4.diff

if ( 'OR' == $meta_query->relation ) { 
    $this->query_fields="DISTINCT " . $this->query_fields; 
}

Tracked down courtesy of this trac ticket.

See if that works…

Leave a Comment