实际上,当你author_name, WP_Query 将使用get_user_by( \'slug\', <author name> ) 要查找具有指定;“好名字”;(user_nicename) 值,如果找到,则WP_Query 设置author arg到找到的用户ID。
因此,您可以遵循相同的方法,但您可以使用get_users() 要查找具有指定名字和/或姓氏的用户,请使用author (或author__in) arg withWP_Query。例如。
$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.
}
附加注释$data = $request->get_params(); 因为您可以通过WP_REST_Request 对象,例如。$request[\'author_name\'] 就像我上面的例子一样。
您应该注册author_name 使用args in the third parameter for register_rest_route(), 或者至少在打电话之前检查它是否为空get_users().
我知道print_r() 在您的代码中只是为了测试,但函数实际上会回显输出,除非第二个参数设置为true..