Wordpress:get_users() with 'all_with_meta' 不提供元数据

Wordpress: get_users() with 'all_with_meta' does not give meta data

这应该很容易,但我无法解决...,您包含值为“all_with_meta”的 arg“字段”,您应该获得用户元数据。我的代码:

$users = get_users([
            'fields'    => 'all_with_meta',
            'role'      => 'colegiado',
            'offset'    => $start,
            'number'      => $limit
        ]);

其中一个结果(它有元数据,但没有显示):

WP_User Object
(
    [data] => stdClass Object
        (
            [ID] => 4
            [user_login] => ccarasm
            [user_pass] => **********
            [user_nicename] => ccarasm
            [user_email] => car*****@****.com
            [user_url] => 
            [user_registered] => 2021-09-10 09:23:26
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => Cass CM
        )

    [ID] => 4
    [caps] => Array
        (
            [no_colegiado] => 1
            [colegiado_inactivo] => 1
            [colegiado] => 1
        )

    [cap_key] => wp_capabilities
    [roles] => Array
        (
            [1] => colegiado_inactivo
            [2] => colegiado
        )

    [allcaps] => Array
        (
            [read] => 1
            [publish_posts] => 1
            [edit_posts] => 1
            [level_0] => 1
            [frm_view_forms] => 1
            [frm_edit_forms] => 1
            [frm_view_entries] => 1
            [frm_create_entries] => 1
            [frm_edit_entries] => 1
            [no_colegiado] => 1
            [colegiado_inactivo] => 1
            [colegiado] => 1
        )

    [filter] => 
    [site_id:WP_User:private] => 1
)

我想从查询中获取每个用户的所有元信息,有人可以帮助我吗?谢谢!!!

** 更新:@Xhynk 给出的解决方案的替代方案**

我需要将它插入到 wp_user 对象中,所以我使用方法 get() 并将元数据添加到对象中:

foreach ($users as $key => $wp_user_object) {
        $wp_user_object->data->num_colegiado = $wp_user_object->get('num_colegiado');
        $wp_user_object->data->first_name = $wp_user_object->get('first_name');
        $wp_user_object->data->last_name = $wp_user_object->get('last_name');
        $wp_user_object->data->telefono = $wp_user_object->get('telefono');
        $wp_user_object->data->nif = $wp_user_object->get('nif');
}

不幸的是,根据 Codex entry for WP_User_Query()`return 值 部分如下:

If ‘fields‘ is set to ‘all’ (default), or ‘all_with_meta’, it will return an array of WP_User objects (does not include related user meta fields even with ‘all_with_meta’ set).

上面的参数部分也有注释:

*’all_with_meta’ currently returns the same fields as ‘all’ which does not include user fields stored in wp_usermeta. You must create a second query to get the user meta fields by ID or use the __get PHP magic method to get the values of these fields.

这意味着目前(从 Q/A 开始,使用 WordPress 5.8.1)您需要单独获取用户元数据,使用 get_user_meta() 函数或魔术方法。例如:

$users = get_users( $args );

foreach( $users as $user ){
    // Get All User Meta Fields as array
    $all_meta_fields = get_user_meta( $user->ID );

    /* OR */
    $field_you_want = get_user_meta( $user->ID, 'field_you_want', true );

    /* OR */
    $field_you_want = $user->key_of_meta_field;
    // example: var_dump( $user->rich_editing; ); // Outputs `string(4) "true"`
}