Site icon Hip-Hop Website Design and Development

User query ā€“ getting values for custom meta keys/fields

I am beginner at customizing wordpress but have some experience in PHP coding.

Iā€™m struggling to figure out how to display all existing meta keys (custom fields defined by ACF) for all users that have role "author".
Default fields display ok, but cannot get values of those meta keys / custom fields. šŸ˜³

The code below was created with wp generator and manually expanded.

I tried here with only 2 default fields (user email and display name) and 2 meta custom fields (medical diagnosis and gender) but I actually need a code for getting specific individual meta custom fields and another version for getting all non empty default and meta fields.

If anyone has time to look šŸ‘Œā¤ļø

$args = array(
    'role'           => 'author',
    'order'          => 'DESC',
    'orderby'        => 'post_count',
    'meta_query'     => array(
        'relation' => 'OR',
        array(
            'key'     => 'medical_diagnosis',
            'compare' => 'EXISTS',
        ),
        array(
            'key'     => 'gender',
            'compare' => 'EXISTS',
        ),
    ),
    'who'            => 'authors',
    'count_total'    => true,
    'fields'         => array( 'user_email', 'display_name'),
);

// The User Query
$users_query = new WP_User_Query( $args );

// The User Loop
if ( ! empty( $users_query->results ) ) {
    foreach ( $users_query->results as $user ) {
        // do something
        $metadata = get_user_meta($user->ID);
        
        echo the_author_meta( 'gender', $user->ID );
        
        foreach($metadata as $meta_key => $meta_value) {
     echo $meta_key.': '.$meta_value.'<br />';
}
        
    $out = '<p>users list:</p>'. $user->user_email . '<br />'. $user->display_name . '<br />'. $metadata['gender'] . '<br />'. $medical_diagnosis. '<br />';
    return $out;
    
    }
} else {
    echo 'no users found';
}