我的朋友让我为他的多作者博客创建一个小部件。他想列出3位在其帖子中拥有最多总帖子浏览量的作者。我已经使用post元数据完成了post视图的工作。但我对用这个标准询问作者感到困惑。有什么想法吗?
谢谢你的帮助。
[更新]我正在使用这些代码及其工作。有没有更简单的版本?
global $wpdb;
$topuser = array();
// query all posts by each user
$users = $wpdb->get_results("SELECT ID FROM $wpdb->users ORDER BY ID");
foreach ( $users as $user ) {
$query = get_posts( array(\'author\' => $user->ID ));
$counter = 0;
// get each post of a user
foreach ( $query as $post ){
$views = absint( get_post_meta( $post->ID, \'post_views_count\', true ) );
$counter += $views;
}
$topuser[] = array( \'id\' => $user->ID, \'views\' => $counter);
wp_reset_query();
}
// function to sort array based on views count
function sortViews($a, $b) {
return $b[\'views\'] - $a[\'views\'];
}
usort($topuser, \'sortViews\'); // sort the array
$output = array_slice($topuser, 0, 3); // slice the array by limit 3
// output the result: user, total postview count, latest post
foreach ($output as $user){
$profile = get_userdata($user[\'id\']);
$query = get_posts( array(\'author\' => $user[\'id\'], \'numberposts\' => 1 ));
echo \'<p>\' . $profile->user_nicename .\' (\'. $user[\'views\'] .\')</p>\';
foreach ( $query as $post ){
echo \'<a href="\' . $post->post_name . \'">\' . $post->post_title . \'</a>\';
echo get_post_meta($post->ID, \'post_views_count\', true);
}
}
SO网友:amit
按帖子总视图列出作者-如果view
是自定义字段名,则此函数将根据帖子的总视图计数按降序返回所有用户。
只需在主题的functions.php
文件调用函数wpse_61194_top_authors(\'view\')
具有meta_key
名称
// Usage -
// $list = wpse_61194_top_authors(\'view\');
// foreach ($list as $li) {
// echo \'<li>\'.$li->user_nicename.\' - \'.$li->view.\'</li>\';
// }
function wpse_61194_top_authors ($meta_key) {
global $wpdb;
$query = "SELECT wp.post_author, wu.user_nicename, SUM(wm.meta_value) as $meta_key
FROM $wpdb->posts wp, $wpdb->postmeta wm, $wpdb->users wu
WHERE wp.ID = wm.post_id
AND wp.post_author = wu.id
AND wm.meta_key = \'$meta_key\'
AND wp.post_type = \'post\'
GROUP BY wp.post_author
ORDER BY $meta_key DESC";
$post_count = $wpdb->get_results($query);
return $post_count;
}