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
  • This is not possible via WP_User_Query on its own as it doesn't count comments in this query, therefore there is no easy orderby 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 using array_count_values) – Bysander Commented Nov 23, 2020 at 17:16
  • @Bysander i understand what you mean but i don't think that i can make this work. could you please be more specific ? like a snippet or somthing else that could help me ... – Artan Commented Nov 23, 2020 at 18:58
  • for anyone who may need this feature too : wordpress.stackexchange/questions/195155/… wordpress.stackexchange/questions/308517/… – Artan Commented Nov 25, 2020 at 14:15
Add a comment  | 

2 Answers 2

Reset to default 0

you 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