admin管理员组文章数量:1314221
i'm using "WP_User_Query" to displaying the most active user orderby = "post_count" in front and It works fine,
now i want to display the most active user based on comment count in front
i will be appreciate it if anyone could help me on this.
i'm using "WP_User_Query" to displaying the most active user orderby = "post_count" in front and It works fine,
now i want to display the most active user based on comment count in front
i will be appreciate it if anyone could help me on this.
Share Improve this question edited Nov 25, 2020 at 14:17 Artan asked Nov 22, 2020 at 20:00 ArtanArtan 32 bronze badges 3 |2 Answers
Reset to default 0you can use this
$user = new wp_user(get_current_user_id());
function 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 '';
echo commentCount();
here is some code I have typed up straight from the codex - I have not tested or run it as I am at work but should put you on the right track. I have included all the pages to learn more about each function / step in the process. Parts of it have been deliberately left longer to help understand what's happening at each stage of this
// Find query params here
// https://developer.wordpress/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/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/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/reference/functions/get_userdata/
$user_obj = get_userdata( $user );
}
// Option two
// - Flips the keys and values round ( https://www.php/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/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
}
本文标签: phpdisplay most active user sort by comment count Solved
版权声明:本文标题:php - display most active user sort by comment count [Solved] 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741964966a2407494.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
WP_User_Query
on its own as it doesn't count comments in this query, therefore there is no easyorderby
parameter to use. You may be able to work something out using something like this - ideally you would get the array of comments only once to build an order of most popular commenters (maybe usingarray_count_values
) – Bysander Commented Nov 23, 2020 at 17:16