wp_user_meta doesn’t return data in a foreach loop

Use this code instead-

$args = array(
    'meta_key' => 'last_name',
    'orderby' => 'meta_value',
    'order' => 'ASC'
);              

$users = get_users( $args );
                
foreach ( $users as $user ) {
    $user_id = $user->ID;
    $post_count = count_user_posts( $user_id ); 
                        
    if ( $post_count > 0 ) {                                          
        $user_meta = get_user_meta( $user_id, '', true );
        echo 'ID: ' . $user_id . ' | ';
        var_dump( $user_meta );
        echo '<br />';
    }
}

Ensure the get_user_meta function retrieves all meta keys by passing an empty string as the meta key and setting the third parameter to true to return the data in a single array.

tech