Order users by user role

时间:2019-07-18 作者:artist learning to code

我在团队页面中显示的用户配置文件中有自定义字段。上面写着“主任”、“研究员”、“毕业生”、“实习生”等。添加新团队成员时,可以从带有选项的选择框中进行选择。

现在,页面按创建日期顺序显示用户,但我需要按层次顺序显示他们(首先是所有董事,然后是研究人员,然后是毕业生,等等)。

配置文件的新字段位于函数中。php,代码如下:

<!-- 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);
}
然后,页面中显示用户的代码如下:

$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

}
    } 
    ?>

2 个回复
最合适的回答,由SO网友:Faham Shaikh 整理而成

@辛西亚拉拉

我想你用的是一个WP_USER_QUERY::__construct 形式或WP_USER_QUERYget_users 或者类似的东西。

您可以使用\'meta_key\' => \'<YOUR_DESIGNATION_META_KEY>\',\'orderby\' => \'meta_value_num, 按元键的值对结果进行排序。

如果您在此meta\\u键中使用了基于文本的值,请考虑使用基于数值的系统。例如:。

由于这将允许您轻松实现所需,并且您可以全局定义指定数组,因此将索引存储为基于所选指定的用户元,并根据存储的元值显示指定

使用代码示例更新

$args = array(
    \'meta_key\'     => \'member_role\',
    \'orderby\'      => \'meta_value_num\',
    \'order\'        => \'ASC\'
); 
$results = get_users( $args );
然后您可以使用foreach

并且不要让“选择”下拉列表的选项值像0、1、2、3等。

SO网友:Oscprofessionals

使用以下代码按角色获取用户

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

相关推荐

Filter to wp_list_authors

我正在使用wp\\u list\\u authors,按照函数名的说法,创建一个作者列表,但我只想显示“作者”。现在在我的网站上,任何发布文本的帐户(“管理员”、“编辑器”)都会在我的作者列表中显示他的名字,但我只希望用户是作者。我怎样才能做到这一点?我的代码现在<?php $args = array( \'orderby\' => \'name\', \'order\' => \'ASC\', \'number\'