$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>User meta query results in PHP notice: only variables should be passed by reference|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

User meta query results in PHP notice: only variables should be passed by reference

matteradmin11PV0评论

I am using a URL rewrite to put the user's display name in the URL to display a user profile. This part all works fine and the proper value is passed through the query_vars and I can get the correct user.

Since this is for a plugin, some plugin users may want to use the user's first/last name instead of the display name. I've set up an opportunity for them to do that where the first/last name is separated by an underscore(_).

To retrieve the user when the URL rewrite is passing the query_var "display_name", I am using a meta query in get_users().

// Get the custom query_var value.
$query_var = get_query_var( 'display_name' );

/**
 * When the display_name query_var is first name/last name,
 * (for example "john_smith"), split the value by unscore 
 * to get the individual first and last name values.
 */
$pieces = explode( '_', $query_var );

// User meta query to get the user ID by first_name/last_name.
$user = reset( get_users( array(
    'meta_query'  => array(
        'relation' => 'AND',
        array(
            'key'=>'first_name',
            'value' => $pieces[0],
            'compare' => '='
        ),
        array(
            'key' => 'last_name',
            'value' => $pieces[1],
            'compare' => '='
        )
    ),
    'number' => 1,
    'count_total' => false
) ) );

The meta query works fine and the correct user ID is returned, but I get a PHP notice as follows:

Notice: Only variables should be passed by reference

The line indicated is the meta query line $user = reset( get_users( array(

Any thoughts on what is wrong and is throwing the PHP notice? Any ideas on a better query to get the user ID by first name/last name meta keys?

I am using a URL rewrite to put the user's display name in the URL to display a user profile. This part all works fine and the proper value is passed through the query_vars and I can get the correct user.

Since this is for a plugin, some plugin users may want to use the user's first/last name instead of the display name. I've set up an opportunity for them to do that where the first/last name is separated by an underscore(_).

To retrieve the user when the URL rewrite is passing the query_var "display_name", I am using a meta query in get_users().

// Get the custom query_var value.
$query_var = get_query_var( 'display_name' );

/**
 * When the display_name query_var is first name/last name,
 * (for example "john_smith"), split the value by unscore 
 * to get the individual first and last name values.
 */
$pieces = explode( '_', $query_var );

// User meta query to get the user ID by first_name/last_name.
$user = reset( get_users( array(
    'meta_query'  => array(
        'relation' => 'AND',
        array(
            'key'=>'first_name',
            'value' => $pieces[0],
            'compare' => '='
        ),
        array(
            'key' => 'last_name',
            'value' => $pieces[1],
            'compare' => '='
        )
    ),
    'number' => 1,
    'count_total' => false
) ) );

The meta query works fine and the correct user ID is returned, but I get a PHP notice as follows:

Notice: Only variables should be passed by reference

The line indicated is the meta query line $user = reset( get_users( array(

Any thoughts on what is wrong and is throwing the PHP notice? Any ideas on a better query to get the user ID by first name/last name meta keys?

Share Improve this question asked Dec 7, 2018 at 14:39 butlerblogbutlerblog 5,1413 gold badges28 silver badges44 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

The value passed to reset() is passed by reference, which means technically it modifies the original variable that's passed to it. For this to work you need to pass a variable, not a function that returns a value.

reset() rewinds array's internal pointer to the first element and returns the value of the first array element.

All you need to do is assign the returned value if get_users() to a variable and then use reset() on that:

$users = get_users( array(
    'meta_query'  => array(
        'relation' => 'AND',
        array(
            'key'=>'first_name',
            'value' => $pieces[0],
            'compare' => '='
        ),
        array(
            'key' => 'last_name',
            'value' => $pieces[1],
            'compare' => '='
        )
    ),
    'number' => 1,
    'count_total' => false
) );

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

The function reset() changes the passed variable, hence it needs a reference. Function output of get_users() can't be referenced because it is not a variable. Just assign the return value to a variable and call that:

$users = get_users( array(
    //...
) );
$user = reset( $users );
Post a comment

comment list (0)

  1. No comments so far