admin管理员组文章数量:1419238
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
}
}
?>
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
}
}
?>
Share
Improve this question
edited Jul 23, 2019 at 17:41
artist learning to code
asked Jul 18, 2019 at 20:23
artist learning to codeartist learning to code
3311 gold badge5 silver badges18 bronze badges
2 Answers
Reset to default 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.
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 ;
}
本文标签: authorOrder users by user role
版权声明:本文标题:author - Order users by user role 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745301406a2652392.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论