按自定义用户元对用户进行排序

时间:2011-10-05 作者:Niels

我正在显示这样的用户列表:

<ul>

<?php   $directors = get_users(\'role=director\'); 

        foreach ($directors as $director) { 

            $dir_id = $director->ID;

            $dir_order = get_user_meta($dir_id, \'exit_director_order\', TRUE);

            $dir_link = get_bloginfo(\'home\').\'/?author=\'.$dir_id; 

            if ($dir_id == $director_id ) {

                $dir_class= \'current director-\'.$dir_id;

            } else { 

                $dir_class= \'director-\'.$dir_id;
            }
?>

           <li>
                <a href="<?php print $dir_link; ?>" class="<?php print $dir_class; ?>"><?php echo $director->display_name; ?></a><br>
           </li>


<?php } ?>


</ul>
我想按dir\\u顺序(从最小到最大)对用户进行排序。这些值作为整数存储在数据库中。

我该怎么做呢?

<小时>

EDIT:

这是解决方案。评论包括:

<?php   $results = get_users(\'role=director\'); 

        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->exit_director_order,
                \'dir_id\'        =>  $user->ID,
                \'dir_name\'      =>  $user->first_name.\' \'.$user->last_name        
            );
        }

        // 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_link = get_bloginfo(\'home\').\'/?author=\'.$director[\'dir_id\']; 


            // The list items
            echo \'<li>\';
            echo \'<a href="\'.$dir_link.\'" id="dir-id-\'.$dir_id.\'">\'.$dir_name.\'</a>\';
            echo \'</li>\';


        } 

        echo \'</ul>\';


?>

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

这是解决方案。评论包括:

<?php   $results = get_users(\'role=director\'); 

        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->exit_director_order,
                \'dir_id\'        =>  $user->ID,
                \'dir_name\'      =>  $user->first_name.\' \'.$user->last_name        
            );
        }

        // 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_link = get_bloginfo(\'home\').\'/?author=\'.$director[\'dir_id\']; 


            // The list items
            echo \'<li>\';
            echo \'<a href="\'.$dir_link.\'" id="dir-id-\'.$dir_id.\'">\'.$dir_name.\'</a>\';
            echo \'</li>\';


        } 

        echo \'</ul>\';


?>

SO网友:Jake

要按自定义元字段对所有用户进行排序,请设置meta_key 和订购人meta_valuemeta_value_num 在这种情况下。

$results = get_users( array(
    \'role\'       => \'director\',
    \'meta_key\'   => \'exit_director_order\',
    \'orderby\'    => \'meta_value_num\',
    \'order\'      => \'ASC\'
) );
EDIT: 这似乎奏效了,也应该奏效。我相信它在下一个版本中会起作用。但就目前而言,它不起作用。

在此选择的答案是我在此之前找到的最佳选择:Ordering users of a specific role by last name

结束

相关推荐

获取GET_USERS函数的用户ID的最佳方式是什么?

我正在尝试按角色类型创建一个用户表。获取用户id的最佳实践方法是什么?我的目标是首先打印每个用户&;姓氏、用户名和电子邮件。我现在拥有的:$get_admins = array( \'blog_id\' => $GLOBALS[\'blog_id\'], \'role\' => \'administrator\', ); $blogusers = get_users($get_admins); foreach ($blog