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 = 'pusers list:/p'. $user-user_email . 'br /'. $user-display_name . 'br /'. $metadata['gender'] . 'br /'. $medical_diagnosis. 'br /';
    return $out;
    
    }
} else {
    echo 'no users found';
}

Topic wp-user-query meta-value user-meta custom-field Wordpress

Category Web


SOLVED. I missed ID field in field array.

$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( 'ID', 'user_email', 'display_name'),
);

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

// The User Loop
if ( ! empty( $users_query->get_results() ) ) {
    foreach ( $users_query->get_results() as $user ) {
        // do something
        
        // get all user meta at once:
        $meta_for_user = get_user_meta( $user->ID);
        
    $out = '<p>users list</p>'. $user->user_email . '<br />'. $user->display_name . $meta_for_user['medical_diagnosis'][0] . '<br />' . $meta_for_user['gender'][0] . '<br />'. $user->ID . $meta_for_user['last_name'][0] .  '<br />';
    return $out;
    
    }
} else {
    echo 'no users found';
}
    
} );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.