用户在WP_USER_QUERY中出现两次

时间:2016-03-10 作者:Bysander

由于某种原因,在使用元过滤器进行用户查询时,我有一个用户print_r 结果在列表中显示两次。。。

我检查了数据库,以防有重复的条目,但用户只有一个值。此外,如果没有元查询,用户只会出现一次。

代码:

$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>\';
输出(以及许多其他输出):

[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] => 
    )
现在我不能很明显地向您显示详细信息,但请注意输出的ID和reg时间以及它们出现的数组编号,[54][55]

没有其他人出现两次。

提前感谢-欢迎您的任何意见,无论大小。

1 个回复
最合适的回答,由SO网友:Adam 整理而成

试试这个吧。。。

解决方案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"; 
}
解决方案2能否尝试添加\'relation\' => \'OR\' 到您的meta\\u查询:

$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\',
             ),
        ),
    )
);
请参见此差异:

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

if ( \'OR\' == $meta_query->relation ) { 
    $this->query_fields = \'DISTINCT \' . $this->query_fields; 
}
这是我的荣幸trac ticket.

看看这是否有效。。。

相关推荐