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?
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 );