Order by first name

时间:2011-09-22 作者:ingvi

我正在尝试按Show\\u name或first name ASC排序成员列表。http://sim.is/sim/felagatal-sim/

这似乎不起作用。有什么建议吗?:/

$users = get_users(array(
    \'role\' => \'sm_flagar\',
    \'orderby\' => \'display_name\',
    \'order\' => \'ASC\',
));

    foreach ($users as $user) {
        $firstName = get_user_meta($user->ID, \'first_name\', true);
        $lastName = get_user_meta($user->ID, \'last_name\', true);
            echo \'<li><a href="\' . $user->user_url . \'">\' . $firstName . \' \' . $lastName . \'</a></li>\' . PHP_EOL;
    }

5 个回复
最合适的回答,由SO网友:Egill R. Erlendsson 整理而成

这是我的解决方案

function split_names( $name ) {
    $names = explode( " ", trim( join( " ", $name ) ) );
    $first_name = array_shift( $names );
    $last_name = array_pop( $names );
    $middle_name = join( " ", $names );

    return  array( "first_name" => $first_name, "middle_name" => $middle_name, "last_name" => $last_name );
}

function sort_by_full_name( $arr1, $arr2 ) {
    $arr1 = split_names( $arr1 );
    $arr2 = split_names( $arr2 );
    $retval = strcoll( $arr1[\'first_name\'], $arr2[\'first_name\'] );
    if ( ! $retval ) $retval = strcoll( $arr1[\'last_name\'], $arr2[\'last_name\'] );
    if ( ! $retval ) $retval = strcoll( $arr1[\'middle_name\'], $arr2[\'middle_name\'] );
    return $retval;
}

$users = get_users( array(
    \'role\' => \'sm_flagar\',
    \'orderby\' => \'display_name\',
    \'order\' => \'ASC\'
) );

$user_list = array();

foreach ( $users as $user ) {
    $firstName = get_user_meta( $user->ID, \'first_name\', true );
    $lastName = get_user_meta( $user->ID, \'last_name\', true );
    $user_list[] = split_names( array( 
            "first_name" => $firstName, 
            "last_name" => $lastName, 
            "url" => $user->user_url 
    ) );
}

usort( $user_list, \'sort_by_full_name\' );
setlocale( LC_COLLATE, $locale );
$locale = setlocale( LC_COLLATE, \'is_IS.utf8\' );

foreach ( $user_list as $user ) {
        echo \'<li><a href="\' . $user["url"] . \'">\' . 
                    $user["first_name"] . \' \' .
                    $user["middle_name"]
                    $user["last_name"] . \'</a></li>\' . PHP_EOL;
}

SO网友:nawelv

原来的代码还不错。只是缺少了meta_key 参数示例:

$theusers = get_users( array(
    \'role\'     => \'contributor\',
    \'meta_key\' => \'first_name\',
    \'orderby\'  => \'meta_value\',
) );

SO网友:Tomas Buteler

现在,只要您试图通过默认的WP usermeta字段进行排序,您就可以这样做:

$users = get_users(array(
    \'fields\' => \'all_with_meta\'
    // Add whatever arguments you need
));

// Sort by first name, ascending
usort($users, create_function(\'$a, $b\', \'if($a->first_name == $b->first_name) { return 0;} return ($a->first_name > $b->first_name) ? 1 : -1;\'));

SO网友:booota

删除“ASC”之后的“,”(不带引号)。它会成功的。

$users = get_users(array(
    \'role\' => \'sm_flagar\',
    \'orderby\' => \'display_name\',
    \'order\' => \'ASC\'
));

    foreach ($users as $user) {
        $firstName = get_user_meta($user->ID, \'first_name\', true);
        $lastName = get_user_meta($user->ID, \'last_name\', true);
            echo \'<li><a href="\' . $user->user_url . \'">\' . $firstName . \' \' . $lastName . \'</a></li>\' . PHP_EOL;
    }
它按要求工作。如果您回显循环中每个用户的显示名称,那么您将了解发生了什么。

将“echo”行替换为:

echo \'<li><a href="\' . $user->user_url . \'">\' . $firstName . \' \' . $lastName . \'</a>\' . $user->display_name . \'</li>\' . PHP_EOL;
它不是按名字和姓氏排序,而是按“显示名称”排序。

SO网友:Jake

要按名字排序所有用户,请设置meta_key 和订购人meta_value.

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

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

结束

相关推荐

自定义WP查询中的orderby不起作用

我有这个问题: $new = new WP_Query(array(\'post_type\'=>\'support\',\'order\'=>\'DESC\', \'meta_key\'=>\'post_views_count\', \'orderby\'=> \'meta_value_num\', \'posts_per_page\'=>\'20\')); 结果集从自定义帖子类型中提取正确数量的帖子support 但是帖子不是按meta\\u值排序的。我无法理