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