0

I need get some meta data for some users. I have meta data with the keys auto and shop and I need get their values. I'm able to access other data related to users using this code:

<?php $roles = array('autobuyer');
$users = array();
foreach ($roles as $role) {
    $args = array('role'=>$role, 'orderby' => 'registered', 'order' => 'ASC', 'search_columns' => 'nicename', 'number'=> 0, 'date_query'  => array(
            'after'     => 'October 14st, 2013',
            'before'    => array(
                'year'  => 2020,
                'month' => 12,
                'day'   => 10,
            )), 'fields' => 'all_with_meta' );
    $usersofrole = get_users($args);
    $users = array_merge($usersofrole,$users);


}

foreach ($users as $user) {
    echo '<li>' . $user->display_name . '</li>';
    echo '<li>' . $user->user_nicename . '</li>';
    echo '<li>' . $user->user_email . '</li>';
    echo '<li>' . $user->user_registered . '</li>';
}?>
0

Use get_user_meta() to get the meta data associated with the users in your loop:

<?php

    $roles = array('autobuyer');
    $users = array();

    foreach ( $roles as $role ) {
        $args = array( 
                        'role'=>$role,
                        'orderby' => 'registered',
                        'order' => 'ASC',
                        'search_columns' => 'nicename',
                        'number'=> 0,
                        'date_query'  => array(
                            'after'     => 'October 14st, 2013',
                            'before'    => array(
                                    'year'  => 2020,
                                    'month' => 12,
                                    'day'   => 10,
                        ) ),
                        'fields' => 'all_with_meta'
                    );

        $usersofrole = get_users( $args );
        $users = array_merge( $usersofrole, $users );
    }

    foreach ( $users as $user ) {

        // Get meta value for 'auto' key.
        $user_auto = get_user_meta( $user->ID, 'auto ', true ); 
        if ( $user_auto ) { 
            echo '<li>' . esc_html( $user_auto ) . '</li>';
        }

        // Get meta value for 'shop' key.
        $user_shop = get_user_meta( $user->ID, 'shop ', true ); 
        if ( $user_shop ) { 
            echo '<li>' . esc_html( $user_shop ) . '</li>';
        }

        // Alternatively, get all user meta at once:
        // $all_meta_for_user = get_user_meta( $user->ID );
        // print_r( $all_meta_for_user );

        echo '<li>' . $user->display_name . '</li>';
        echo '<li>' . $user->user_nicename . '</li>';
        echo '<li>' . $user->user_email . '</li>';
        echo '<li>' . $user->user_registered . '</li>';
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy