Get user by meta key - WP multi site

I am trying to get a user by metakey:$user = get_users( array ('meta_key' => 'token','me

I am trying to get a user by metakey:

        $user = get_users( array (
            'meta_key'     => 'token',
            'meta_value'   => $token,
            'orderby'      => 'meta_value',
            'order'        => 'DESC',
            'number'       => '10',
        ) );

This returns empty array, but there is a user with that meta key value.

    $user = reset(
            get_users(
                array(
                    'meta_key' => 'token',
                    'meta_value' => $token,
                    'number' => 1,
                    'count_total' => false
                )
            )
        );

This returns false with a notice:

only variables should be passed by reference in

, but still there is a user with that value for the meta.

I need just to select a user FROM the current blog (to check, is it exists ) by metavalue. User in WordPress NETWORK, which is not attached to the current blog.

I am trying to get a user by metakey:

        $user = get_users( array (
            'meta_key'     => 'token',
            'meta_value'   => $token,
            'orderby'      => 'meta_value',
            'order'        => 'DESC',
            'number'       => '10',
        ) );

This returns empty array, but there is a user with that meta key value.

    $user = reset(
            get_users(
                array(
                    'meta_key' => 'token',
                    'meta_value' => $token,
                    'number' => 1,
                    'count_total' => false
                )
            )
        );

This returns false with a notice:

only variables should be passed by reference in

, but still there is a user with that value for the meta.

I need just to select a user FROM the current blog (to check, is it exists ) by metavalue. User in WordPress NETWORK, which is not attached to the current blog.

Share Improve this question asked Jul 6, 2019 at 23:27 gdfgdfggdfgdfg 1721 silver badge15 bronze badges 1
  • 1 I noticed you passed the result of get_users into a reset call, can you explain why? It's not good practice to nest function calls as it makes it difficult to debug problems. Have you verified $token actually contains the correct value? How are you error checking the value of get_users to make sure it isn't a WP_Error object with an error message? – Tom J Nowell Commented Jul 7, 2019 at 1:55
Add a comment  | 

1 Answer 1

Reset to default 1

This is the problem:

$user = reset(
        get_users(

reset expects a reference to an array, and there's no mechanism for error checking. So even if it finds the user, you're misusing the return value.

It might be possible to adjust this to use references properly, but references in PHP are something best avoided, and there are better options out there for getting the first item in an array in PHP.

This would work better, and provide a chance to check for error values or empty arrays:

$users = get_users(... );
$user = reset( $users );

This would work even better as it doesn't modify the array, and it runs in O(1) time so it's the same cost no matter how large the array gets:

$users = get_users(...);
$user = array_pop(array_reverse($users));

This one also works in PHP 5.4+:

$users = get_users(...);
$user = array_values($users)[0];

All of them, including what you have right now, will require you to do this:

if ( empty( $users ) ) {
    // nothing was found, print an error message and abort
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745339983a4623281.html

相关推荐

  • Get user by meta key - WP multi site

    I am trying to get a user by metakey:$user = get_users( array ('meta_key' => 'token','me

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信