4

For some reason during a user query with a meta filter I have a user who when I print_r the results appears twice in the list...

I have checked the database, just in case there seemed to be duplicate entries but there is only a single value for the user. Also without the meta query the user only appears once.

Code:

$user_query = new WP_User_Query( 
    array(
        'role'       => 'member',
        'orderby'    => 'registered',
        'order'      => 'DESC',
        'meta_query' => array(
             array(
                 'key'   => 'onTrial',
                 'value' => '1',
             ),
        ),
    )
);
echo '<pre>';
print_r($user_query->results);
echo '</pre>';

Outputs (along with many others):

[54] => WP_User Object
    (
        [data] => stdClass Object
            (
                [ID] => 10410
                [user_login] => [email protected]
                [user_pass] => $P$XXXXXXX
                [user_nicename] => XXXXXXX-XXXXXXX
                [user_email] => [email protected]
                [user_url] => 
                [user_registered] => 2016-02-15 10:52:57
                [user_activation_key] => 
                [user_status] => 0
                [display_name] => XXXXXXX XXXXXXX
            )

        [ID] => 10410
        [caps] => Array
            (
                [member] => 1
            )

        [cap_key] => wp_capabilities
        [roles] => Array
            (
                [0] => member
            )

        [allcaps] => Array
            (
                [read] => 
                [no_admin_bar] => 1
                [member] => 1
            )

        [filter] => 
    )

[55] => WP_User Object
    (
        [data] => stdClass Object
            (
                [ID] => 10410
                [user_login] => [email protected]
                [user_pass] => $P$XXXXXXX
                [user_nicename] => XXXXXXX-XXXXXXX
                [user_email] => [email protected]
                [user_url] => 
                [user_registered] => 2016-02-15 10:52:57
                [user_activation_key] => 
                [user_status] => 0
                [display_name] => XXXXXXX XXXXXXX
            )

        [ID] => 10410
        [caps] => Array
            (
                [member] => 1
            )

        [cap_key] => wp_capabilities
        [roles] => Array
            (
                [0] => member
            )

        [allcaps] => Array
            (
                [read] => 
                [no_admin_bar] => 1
                [member] => 1
            )

        [filter] => 
    )

Now I can't show you the details obviously but note the ID and reg time outputted also the array numbers they appear in, [54] and [55]

No one else appears twice.

Thanks in advance - any input appreciated, small or large.

5
  • 4
    Does the meta_key/value pair exist twice for the same user in the usermeta table by chance?
    – Adam
    Commented Mar 10, 2016 at 13:29
  • 1
    That could explain it, since the generated query is joining the user meta table with the users table, without any distinct or group by.
    – birgire
    Commented Mar 10, 2016 at 13:34
  • Right - yes. Great shout. It appears twice against that user with different umeta_ids despite that particular bit of code using update_user_meta as an insert command. I didn't spot it before as to check I was using foreach( [get_user_meta($profileuser->ID)] as $key => $value) { How will that have happened?
    – Bysander
    Commented Mar 10, 2016 at 13:39
  • 1
    hmm... typically that would happen if you are using add_user_meta and didn't specify unique. I need to give it some more thought.
    – Adam
    Commented Mar 10, 2016 at 13:44
  • I can obviously delete the entry from the database. However I'd like to know how that crept in there in the first place. That particular entry is made by a daily wp_cron. If it's adding multiple values on the user meta then that query sort of breaks down as useless and relies me spotting duplicates manually or using array_unique for everything... Which is annoying.
    – Bysander
    Commented Mar 10, 2016 at 13:46

1 Answer 1

4

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...

12
  • 1
    I think the posts_distinct only works on WP_Query
    – birgire
    Commented Mar 10, 2016 at 13:42
  • 2
    @userabuser Sometimes I wish there would be concistancy among the query classes regarding the sql filters.. It looks like the trac ticket was closed due to inactivity but I'm not sure it should have the "fixed" status ;-) The DISTINCT is added if there's an OR in the meta query aka $this->meta_query->has_or_relation().
    – birgire
    Commented Mar 10, 2016 at 14:01
  • 1
    This is still an issue 4 years later if one of your meta_query conditions matches a field for a user who has multiple rows with that meta key. For example if a user has two "program" keys that both match "> 0" they will return twice. The OR trick still works but I do not understand why DISTINCT isn't just in there by default. Such a wacky workaround! Commented Apr 23, 2020 at 9:15
  • 1
    @RadleySustaire welcome to WordPress and if you've been developing on WP for a long time then, welcome to WordPress 😂. I'd be interested to know if there is a trac ticket lurk... wait a minute, deja-vu → core.trac.wordpress.org/ticket/17582 (having a giggle) but birgire said it best above "We could just call this OR feature an Easter Egg in WordPress;-)"
    – Adam
    Commented Apr 25, 2020 at 11:51
  • 2
    @Adam Speaking of deja vu... I'm back! Trying to fix the same problem on a different site. And stumbled upon this answer I already posted to! LOL. For next time here's a simple function + filter to drop in on a site which should fix the problem: gist.github.com/RadGH/877ee20d99eee73fb0e933f199d4b8fb Commented May 23, 2020 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.