1

尝试按名字的字母顺序对 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 }
} ?>

多谢你们!

4

1 回答 1

2

您可以使用WP_User_Query 类的 Order 和 Orderby 属性来检索已为每个角色排序的每个数组:

$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");
于 2013-04-25T23:05:27.287 回答