首先,您可以替换
\'posts_per_page\' => \'10000000000\'
使用
\'posts_per_page\' => -1
取消限制。
如果要显示用户排名,可以添加$rank
foreach循环中的计数器:
$rank=0;
// output the result: user, total postview count, latest post
foreach ($output as $user){
$rank++;
$profile = get_userdata($user[\'id\']);
$query = get_posts( array(\'author\' => $user[\'id\'], \'numberposts\' => 1 ));
// update the rank for each user
update_user_meta( $user[\'id\'], \'user_rank\', $rank );
echo \'<div class="user-item">\';
echo \'<p>\' . $profile->user_nicename .\' (\'. $user[\'views\'] .\') [User Rank: \'.$rank.\']</p>\';
foreach ( $query as $post ){
echo \'<a href="\' . $post->post_name . \'">\' . $post->post_title . \'</a>\';
echo get_post_meta($post->ID, get_post_views(\'normal\'), true);
}
echo \'</div>\';
}
更新:计算所有用户的排名可能是cron服务的工作,例如:
wp cron、linux cron、外部cron服务自定义cron对于第2部分和第3部分,这里只有一个想法:
创建一个名为mycron.php
从这里开始,在文件顶部添加两行代码:http://codex.wordpress.org/Integrating_WordPress_with_Your_Website将代码添加到mycron.php
增加array_slice
限制(当前仅设置为10)
运行mycron.php
要测试它,请添加mycron.php
向cron服务(如每日)添加时间/内存测量值mycron.php
如果这项任务太重,尝试将其分配给其他cronjobs(每个cronjob可能有500个用户)喝一大杯咖啡;-)ps:记住要备份数据库。
更新2:mycron.php
文件,如果要在Wordpress根文件夹中试用:
<?php
define(\'WP_USE_THEMES\', false);
require(\'./wp-blog-header.php\');
global $wpdb;
$topuser = array();
$html = "";
// 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, \'posts_per_page\' => -1));
$counter = 0;
// get each post of a user
foreach ( $query as $post ){
$views = absint( get_post_views(\'normal\') );
$counter += $views;
}
$topuser[] = array( \'id\' => $user->ID, \'views\' => $counter);
}
// function to sort array based on views count
function sortViews($a, $b) {
return $b[\'views\'] - $a[\'views\'];
}
usort($topuser, \'sortViews\'); // sort the array
// output the result: user, total postview count, latest post
foreach ($topuser as $user){
$rank++;
$profile = get_userdata($user[\'id\']);
$query = get_posts( array(\'author\' => $user[\'id\'], \'numberposts\' => 1 ));
// update the rank for each user
update_user_meta( $user[\'id\'], \'user_rank\', $rank );
$html .= \'<div class="user-item">\';
$html .= \'<p>\' . $profile->user_nicename .\' (\'. $user[\'views\'] .\') [User Rank: \'.$rank.\']</p>\';
foreach ( $query as $post ){
$html .= \'<a href="\' . $post->post_name . \'">\' . $post->post_title . \'</a>\';
$html .= get_post_meta($post->ID, get_post_views(\'normal\'), true);
}
$html .= \'</div>\';
}
echo $html;
printf(" Perfomance: %s queries in %s seconds ", get_num_queries(), timer_stop(0));
echo (file_put_contents("/path/to/ranks.html", $html)>0)? "Success!":"Problem writing to file!";