User query – getting values for custom meta keys/fields

Question

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

0
Vesna Mačković 8 months 2022-02-03T17:06:48-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse