最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get user by meta key - WP multi site

programmeradmin1浏览0评论

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
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论