从所有作者的累计帖子查看计数的查询中显示个人作者排名

时间:2013-04-07 作者:jspecs

我原本打算发帖,问我如何根据作者所有帖子的帖子浏览量对一个列表进行排名。但我幸运地在这里找到了一个解决方案:List users with the most total posts view

这是我在该页面上使用的代码,只是我对其进行了一些调整,以便从我使用的插件中获取帖子视图:

<?php 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,  \'posts_per_page\' => \'10000000000\'));
    $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);
    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, 10); // 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, get_post_views(\'normal\'), true);
    }
} ?>
现在,当我第一次提出这个想法时,我想让这个解决方案朝着我的愿景迈进一步。

我想在buddypress个人资料页面上显示每个用户的具体排名(列表中的位置)。我会将此代码粘贴到成员标题中。php

示例:它会说“User Rank:15”,这意味着在执行上述查询时,该用户显示为第15个结果(当然,我会将数字从10改为更多,这就是为什么可能的原因)。

希望你们能帮忙。提前感谢:)

1 个回复
最合适的回答,由SO网友:birgire 整理而成

首先,您可以替换

\'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

创建一个名为mycron.phphttp://codex.wordpress.org/Integrating_WordPress_with_Your_Websitemycron.phparray_slice 限制(当前仅设置为10)

  • 运行mycron.php 要测试它,请添加mycron.php 向cron服务(如每日)添加时间/内存测量值mycron.php
  • 喝一大杯咖啡;-)
  • 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!";
    

    结束

    相关推荐