Actually, when you set author_name
, WP_Query
will use get_user_by( 'slug', <author name> )
to find a user with the specified “nice name” (user_nicename
) value, and if found, then WP_Query
sets the author
arg to the found user’s ID.
Therefore you can follow the same approach, but you would use get_users()
to find users having the specified first and/or last name and use the author
(or author__in
) arg with WP_Query
. E.g.
$users = get_users( array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $request['author_name'],
'compare' => 'LIKE',
),
array(
'key' => 'last_name',
'value' => $request['author_name'],
'compare' => 'LIKE',
),
),
'fields' => 'ID',
) );
if ( ! empty( $users ) ) {
$query = new WP_Query( array( 'author' => $users[0] ) );
// or to include all found users..
// $query = new WP_Query( array( 'author__in' => $users ) );
// ... your code.
}
Additional Notes
-
You don’t have to do the
$data = $request->get_params();
because you could just access parameters via direct array access on theWP_REST_Request
object, e.g.$request['author_name']
like in my example above. -
You should register
author_name
usingargs
in the third parameter forregister_rest_route()
, or at least, check that it’s not empty before callingget_users()
. -
I know the
print_r()
in your code is just for testing, but the function will actually echo the output unless if the second parameter is set totrue
..