如何按名字按字母顺序对此循环进行排序?

时间:2013-04-25 22:55:12

标签: php wordpress

尝试按名字的字母顺序对WordPress用户进行排序。标准WP_User_query不起作用,因为我需要组合三个不同的用户角色,如您所见。有关如何按用户名字按字母顺序对此列表进行排序的任何想法?我不知道该怎么做!

<?php

// get the featured editors
$editor_query = new WP_User_Query( array( 'role' => 'Editor' ) );
$editors = $editor_query->get_results();

// get the featured authors
$author_query = new WP_User_Query( array( 'role' => 'Author' ) );
$authors = $author_query->get_results();

// get the featured managers
$manager_query = new WP_User_Query( array( 'role' => 'Manager' ) );
$managers = $manager_query->get_results();

// store them all as users
$user_query = array_merge( $authors, $editors, $managers );

// User Loop
if ( !empty( $user_query ) ) {

$sortArray = array();

foreach ( $user_query as $user ) { 
    $auth_id = get_userdata($user->ID);
    $job_title = get_the_author_meta( 'job_title', $user->ID );
    $avatar = get_avatar($user->ID, 87);
    $post_count = get_usernumposts($user->ID);
    $author_profile_url = get_author_posts_url($user->ID);
?>

    <div class="wp-author-wrap">
    <div class="wp-team-box">

        <a href="<?php echo $author_profile_url; ?>"><?php echo $avatar; ?></a>
        <a href="<?php echo $author_profile_url; ?>" class="wp-author-title"><?php echo get_user_meta( $user->ID, 'first_name', TRUE ) . ' ' . get_user_meta( $user->ID, 'last_name', TRUE ); ?></a>
        <?php echo $job_title; ?>
        <ul>

            <li><a href="<?php echo $author_profile_url; ?>">View <?php echo get_user_meta( $user->ID, 'first_name', TRUE ); ?>'s Profile</a></li>
            <li><a href="<?php bloginfo( 'url' ); ?>/?s=+&author=<?php echo $user->ID; ?>">Read <?php echo get_user_meta( $user->ID, 'first_name', TRUE ); ?>'s Posts</a></li>

        </ul>

    </div>

    <div class="wp-dots"></div>
    </div>

<?php }
} ?>

谢谢你们!

1 个答案:

答案 0 :(得分:2)

您可以使用Order and Orderby properties of the WP_User_Query class检索已为每个角色排序的每个数组:

$editor_query = new WP_User_Query(
                    array(
                        'role'    => 'Editor',
                        'orderby' => 'display_name',
                        'order'   => 'ASC'
                    )
                );

然后像这样使用PHP usort

function compare_user_display_name($a, $b) {
    return strcasecmp($a->display_name, $b->display_name);
}

$user_query = array_merge( $authors, $editors, $managers );
usort($user_query, "compare_user_display_name");