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

Time:10-06

It is supposed to be easy but I can not work it out..., you include arg "fields" with value "all_with_meta" and you should get the user metas. My code:

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

One of the results (it has metas, but they do not show up):

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
)

I would like to get all the metas for every user from the query, can anybody help me? Thanks!!!!

** UPDATE: AN ALTERNATIVE TO THE SOLUTION GIVEN BY @Xhynk**

I needed to insert it into wp_user object so I used method get() and added metas to the object:

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');
}

CodePudding user response:

Unfortunately, according to the Codex entry for WP_User_Query()` , under the return values sections is following:

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).

There's also a note in the Parameters section above:

*’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.

What that means, is that currently (as of this Q/A, with WordPress 5.8.1) you'll need to grab the user meta separately, either with the get_user_meta() function or the magic methods. For instance:

$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"`
}
  • Related