0

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

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.