开发者

Wordpress: list authors

I want wordpress to give me an array that contains all 开发者_如何学Pythonauthors. There are some dumb functions, that echo html (wp_list_authors()) - but that's not what I want.

I want to get the full profile (ID, name, meta-data).

My current approach isn't, what I am looking for - to be exact, it's **it.

$authors = wp_list_authors(array(
'optioncount'   => false, 
'exclude_admin' => false, 
'show_fullname' => false,
'hide_empty'    => false,
'echo'          => false,
'html'      => false)
);
$authors = explode(", ", $authors);

And even here I am stuck, since there is no get_author_id_by_name() or similar.

Please help, I don't want to do SQL in a friggin template.


In case someone is still looking, this is how you return your authors in an array the correct way.

function get_all_authors() {
global $wpdb;

foreach ( $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row ) :
    $author = get_userdata( $row->post_author );
    $authors[$row->post_author]['name'] = $author->display_name;
    $authors[$row->post_author]['post_count'] = $row->count;
    $authors[$row->post_author]['posts_url'] = get_author_posts_url( $author->ID, $author->user_nicename );
endforeach;

return $authors;
}

The returned array ($authors) includes the user's ID as the key and also contains author link, post count, and their name. Of course, in your theme's view, you can set get_all_authors() to a variable and loop thru it however you'd like.

Hope this helps!


Well, if you know the user ID, you can use get_userdata.

Otherwise you should take a look at this suggestion: http://wpengineer.com/list-all-users-in-wordpress/


You should better use get_users, it's in the codex.


global $wpdb;
$allAuthorIds = $wpdb->get_column( "SELECT ID FROM {$wpdb->users}" );
$authorsInfo = array();
foreach( $allAuthorIds as $authorId ) {
  $authorsInfo[$authorId] = get_userdata($authorId);
}

That will give the info for every single user (including WP extended info) in the $authorsInfo array. If you need to get users by their role ('author') then you should use the WP_User_Search class. You're pretty much forced to use SQL here and there is nothing wrong with that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜