0

I have custom fields in the user profile that displays in the team page. It reads "director" "researcher" "graduate" "intern" and some others. When adding a new team member, you pick from a select box with the options.

Right now the page displays the users in date of creation order but I need to show them in hierarchy order (all directors first, then the researchers, then graduate, etc, etc).

The new fields for the profile are in functions.php with the following code:

<!-- ROLE -->

<?php $role = get_user_meta($user->ID, 'member_role', true); ?>

<table class="form-table">

<tr>
    <th><label for="member_role">Lab Role</label></th>
    <td>
        <select name="member_role" id="member_role">
        <option value="" <?php if($role == ''){echo('selected="selected"');}?>>Choose role</option>
        <option value="principal-investigator" <?php if($role == 'principal-investigator'){echo('selected="selected"');}?>>Principal Investigator</option>
        <option value="labmanager" <?php if($role == 'labmanager'){echo('selected="selected"');}?>>Lab Manager</option>
        <option value="administrativeassistant" <?php if($role == 'administrativeassistant'){echo('selected="selected"');}?>>Administrative Assistant</option>
        <option value="postdoc" <?php if($role == 'postdoc'){echo('selected="selected"');}?>>Postdoctoral Fellow</option>
        <option value="gradstudent" <?php if($role == 'gradstudent'){echo('selected="selected"');}?>>Graduate Student</option>
        <option value="researchtech" <?php if($role == 'researchtech'){echo('selected="selected"');}?>>Research Technician</option>
        <option value="undergradstudent" <?php if($role == 'undergradstudent'){echo('selected="selected"');}?>>Undergraduate Student</option>
        <option value="labsupport" <?php if($role == 'labsupport'){echo('selected="selected"');}?>>Lab Support</option>
        </select>
        <br />
        <span class="description">Please select your role at the lab.</span>
    </td>
</tr>

</table>
<?php }

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
add_action('user_register', 'my_save_extra_profile_fields');
add_action('profile_update', 'my_save_extra_profile_fields');

function my_save_extra_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )
    return false;

update_usermeta( absint( $user_id ), 'degrees_and_affiliations', wp_kses_post( $_POST['degrees_and_affiliations'] ) );
update_usermeta( absint( $user_id ), 'member_role', wp_kses_post( $_POST['member_role'] ) );

$items = array('principal-investigator', 'labmanager', 'administrativeassistant', 'postdoc',  'gradstudent', 'researchtech', 'undergradstudent', 'labsupport' );
$role = get_user_meta($user_id, 'member_role', true);
$order = array_search($role, $items);

update_user_meta( absint( $user_id ), 'lab_member_order', $order);
}

Then the code that shows the users in the page is as follows:

$results = get_users(); 

    foreach ($results as $result) {

        // Get data about each user as an object
        $user = get_userdata($result->ID); 


        // Create a flat array with only the fields we need
        $directors[$user->ID] = array(
            'dir_order'     =>  $user->menu_order,
            'dir_id'        =>  $user->ID,
            'dir_name'      =>  $user->first_name.' '.$user->last_name,    
            'dir_email'     =>  $user->user_email,
        );
    }

    // Sort
    sort($directors); 

    // The list
    echo '<ul id="rightcolumndirector">';

    // For each result


    foreach ($directors as $director) { 

        // Set up the variables
        $dir_id = $director['dir_id'];
        $dir_order = $director['dir_order'];
        $dir_name = $director['dir_name'];
        $dir_email = $director['dir_email'];
        $dir_link = get_bloginfo('home').'/?author='.$director['dir_id']; 
        $dir_status = get_field('alumni', 'user_'.$dir_id);
if ($dir_status == 0 && $dir_id !== 24) { ?>
        <div class="author-nucleus">
            <a href="<?php echo get_author_posts_url( $dir_id ); ?>">
                <div class="author-avatar">
                    <div class="hexa">
                        <div class="hex1">
                            <div class="hex2">
                                <?php echo get_wp_user_avatar( $dir_email, 'large' ); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
                <div class="author-info">
                    <h2>
                        <a class="author-name" href="<?php echo get_author_posts_url( $dir_id ); ?>">
                            <?php echo $dir_name; ?>
                        </a><?php
                                if($dir_email != '')
                                {
                                    printf('<a href="mailto:%s">%s</a>', $dir_email, '<span class="dashicons dashicons-email-alt"></span>');
                                }
                            ?>
                    </h2>
                    <hr />
                    <?php 
                        get_member_role($dir_id);
                    ?>
                    <ul class="nucleus-icons-test">
                        <li>
                            <div>
                            <img src="<?php $user_icon = get_field('user_icon', 'user_'.$dir_id); 
                                    echo $user_icon['url']; ?>" />
                            <span><?php echo $dir_name; ?></span>
                            </div>
                        </li>
                    <?php   
                        get_subjects($dir_id, 'post', 4);
                    ?>
                    </ul>
                </div>
            </div>

<?php

}
    } 
    ?>
1

@CynthiaLara

I suppose that you are using a container of WP_USER_QUERY::__construct in the form or WP_USER_QUERY or get_users or something to that effect.

You can use 'meta_key' => '<YOUR_DESIGNATION_META_KEY>','orderby' => 'meta_value_num, to get results sorted by your meta key's value.

If you have used a texted based value in for this meta_key, please consider using a numeric value based system. eg.

  • 0 = 'Director'
  • 1 = 'CTO' and so on as this will allow you to achieve what you want with ease and you can event have the designation array globally defined, store the index as user meta based on selected designation and show the designation based on the stored meta value.

Good luck!!

Update with code sample

$args = array(
    'meta_key'     => 'member_role',
    'orderby'      => 'meta_value_num',
    'order'        => 'ASC'
); 
$results = get_users( $args );

and then you can iterate with your foreach loop.

And do not that the option values for the select drop down must be like 0,1,2,3 etc.

  • Thank you :) I'm using the get_users and will try this. Question tho..if there are several people with the same numeric meta value, will it work and display them together or will it break the query? – Cynthia Lara Jul 19 '19 at 16:09
  • Do not worry, the query will not break in that case. – Faham Shaikh Jul 19 '19 at 17:25
  • I am working on someone else's code so I'm a little stuck and I appreciate your help already, but I do need more help (sorry!) The new values for the users are added in the functions file, using the get_the_author_meta in html select boxes. I will update my question with the code because I'm a little lost on how to implement the meta key :x – Cynthia Lara Jul 23 '19 at 17:38
  • Hey I'm sorry I missed that edited your answer! thank you very much! I tried using the args but apparently that was not necessary, just using numeric values in the select and changing 'dir_order' => $user->menu_order to 'dir_order' => $user->member_role was enough! I couldnt have done it without you tho so I'm giving the answer to you :) – Cynthia Lara Jul 24 '19 at 23:18
0

use below code to get user by role

global $wpdb;
$blog_id = get_current_blog_id();
$user_query = new WP_User_Query( array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => 'customer', //change your user role here which you want to display
'compare' => 'like'
),
array(
'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => 'subscriber',
'compare' => 'like'
)
)
) );
foreach ( $user_query->results as $user ) {
echo "<br/>";
echo "ID:".$user->ID;
echo ' Username:' . $user->user_login ;
}

Your Answer

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

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