我自己做了一些东西:
Page template file:
<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$paged -= 1;
$limit = 20;
$offset = $paged * $limit;
$args = array(
\'number\' => $limit,
\'offset\' => $offset,
);
// Create the WP_User_Query object
global $wp_query;
$wp_query = new WP_User_Query($args);
// Get the results
$authors = $wp_query->get_results();
if($authors): ?>
<div id="staffmembers" class="clearfix">
<?php foreach($authors as $author) : ?>
<a href="<?php echo get_author_posts_url($author->ID); ?>" class="author"><?php echo get_avatar($author->ID, 96); ?><?php echo $author->display_name; ?></a>
<?php endforeach; ?>
</div>
<?php else: ?>
<div class="post">
<p>Sorry, no posts matched your criteria.</p>
</div>
<?php endif; ?>
<?php wp_pagenavi(); ?>
请注意,我使用的是WP PageNavi的alpha版本,它支持WP\\u User\\u查询的分页。上述内容仅涉及布局和分页。下面是魔术发生的地方:
functions.php file:
add_action(\'pre_user_query\', \'sort_connect_author_list_by_last_name\' );
function sort_connect_author_list_by_last_name(&$object) {
global $wp, $wpdb;
if(preg_match( "/^connect(\\/.+)?$/", $wp->request )) {
// Sub query
$sql = "
(SELECT
`{$wpdb->users}`.`ID` AS `c_user_id`,
MAX(CASE WHEN `{$wpdb->usermeta}`.`meta_key` = \'location\' THEN `{$wpdb->usermeta}`.`meta_value` END ) AS `c_location`,
MAX(CASE WHEN `{$wpdb->usermeta}`.`meta_key` = \'last_name\' THEN `{$wpdb->usermeta}`.`meta_value` END ) AS `c_last_name`,
MAX(CASE WHEN `{$wpdb->usermeta}`.`meta_key` = \'first_name\' THEN `{$wpdb->usermeta}`.`meta_value` END ) AS `c_first_name`
FROM
`{$wpdb->users}`
LEFT JOIN `{$wpdb->usermeta}` ON ( `{$wpdb->users}`.`ID` = `{$wpdb->usermeta}`.`user_id` )
WHERE
1=1
GROUP BY
`{$wpdb->users}`.`ID`
) AS `C`
";
// Modify existing query
$object->query_fields = "SQL_CALC_FOUND_ROWS `{$wpdb->users}`.`ID`, `{$wpdb->users}`.`display_name`";
$object->query_from .= " RIGHT JOIN {$sql} ON ( `{$wpdb->users}`.`ID` = `C`.`c_user_id` )";
$object->query_orderby = "ORDER BY `C`.`c_last_name` ASC";
}
}
上面的查询效率不高,在大用户群中速度会非常慢。在我的情况下,我最多运行250个用户。有更好的方法可以做到这一点,但我想用WP PageNavi维护分页。