简体   繁体   中英

Wordpress plugin- rank users by number of posts

I want to create a simple plugin for wordpress -rank users based of number of user posts. Already i have this code and i don't know how to sort array and display it in table

global $wpdb; 
$result = count_users();
$users = $result['total_users'];

for($id = 1;$id<$users;$id++){ 
    $result = $wpdb->get_results("SELECT wp_users.ID, wp_users.display_name, COUNT(wp_posts.post_author) AS 'Number_of_posts' 
        FROM wp_users INNER JOIN wp_posts ON wp_users.ID = wp_posts.post_author 
        WHERE wp_posts.post_type = 'post'             
        AND wp_users.ID = $id", ARRAY_A);
}

Array sorting in PHP: http://php.net/manual/en/array.sorting.php

You could iterate through the WPDB object and do whatever logic you need and put values into an associative array. Sort it using the appropriate function from the PHP docs linked

Displaying data from an array in an html table would look something like this:

<table>
   <tr>
       <th>Rank</th>
       <th>Username</th>
       <th>Post Count</th>
   </tr>
<?php
$i = 1;
foreach($array as $value){?>
       <tr>
          <td><?php echo $i; ?></td>
          <td><?php echo $value['user']; ?></td>
          <td><?php echo $value['posts']; ?></td>
       </tr>
<?php 
      $i++;
} ?>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM