按评论计数显示最活跃的用户排序[已解决]

时间:2020-11-22 作者:Artan

我正在使用;WP\\u User\\u Query“;显示最活跃的用户医嘱依据;post\\U计数“;在前面,效果很好,

现在,我想根据前面的评论数显示最活跃的用户

如果有人能在这方面帮助我,我将不胜感激。

2 个回复
最合适的回答,由SO网友:Mehmet Cemil 整理而成

你可以用这个

$user = new wp_user(get_current_user_id());
函数commentCount(){

global $user;
global $wpdb;
$count = $wpdb->get_var(\'SELECT COUNT(comment_ID) FROM \' . $wpdb->comments. \' WHERE comment_author_email = "\' . $user->data->user_email . \'" and comment_approved = 1\');
return $count;
}

回声“”;

echo commentCount();

SO网友:Bysander

这是我直接从抄本中键入的一些代码——我没有在工作时测试或运行它,但应该会让您走上正确的轨道。我已经包括了所有页面,以了解流程中每个功能/步骤的更多信息。它的一部分被故意留了更长的时间,以帮助理解在这一过程的每个阶段发生了什么

// Find query params here
// https://developer.wordpress.org/reference/classes/wp_comment_query/__construct/
$args = array(
    \'type\'   => \'comment\',
    \'status\' => \'approve\',
);
// Get all approved comments
$comment_arr = get_comments( $args );

// Change array of comments into array of User IDs
// https://developer.wordpress.org/reference/functions/wp_list_pluck/
$list_users = wp_list_pluck( $comment_arr, \'user_id\' );

// This will change the array of User IDs into another array this time containing the number of times a value appears
// https://www.php.net/manual/en/function.array-count-values.php
$totals = array_count_values( $list_users );

// Option one - loop through the totals array to get the user data from each user ID returned in the key
foreach ( $totals as $user => $n_of_comments ) {
    // https://developer.wordpress.org/reference/functions/get_userdata/
    $user_obj = get_userdata( $user );
}

// Option two
// - Flips the keys and values round ( https://www.php.net/manual/en/function.array-flip.php ) then
// Implode array to get a list of User IDs to pass to a single query ( https://www.php.net/manual/en/function.implode.php )
$list_in_order = implode( \',\', array_flip( $totals ) );

$args  = array(
    \'include\' => $list_in_order,
    \'orderby\' => \'include\',
);
$users = get_users( $args );
    foreach ( $users as $user ) {
    // code
}

相关推荐

站点运行状况:检测到活动的PHP会话

我是WordPress开发的新手,我刚刚创建了我的第一个主题。在我的wp admin中"Site Health" 告诉我已检测到PHP会话(严重错误),下面是消息:PHP会话是通过session\\u start()函数调用创建的。这会干扰REST API和环回请求。在发出任何HTTP请求之前,会话应该由session\\u write\\u close()关闭。我需要PHP的$_SESSION 对于主题脚本,我将其添加到我的函数中。要正确初始化会话的php文件:<?php&#x