WordPress.org

Ready to get started?Download WordPress

Forums

List authors by posts? (6 posts)

  1. igneous
    Member
    Posted 5 years ago #

    I want to list authors by the amount of posts they made, is this possible? I couldnt see a way to do it in the docs

  2. MichaelH
    Member
    Posted 5 years ago #

    Don't know how efficient this is:

    <?php
    //Get users and count of posts put into array $uc, sort array by post count descending, loop through array and echo user info
    $uc=array();
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $post_count = get_usernumposts($bloguser->user_id);
        $uc[$bloguser->user_id]=$post_count;
      }
      arsort($uc); //use asort($uc) if ascending by post count is desired
      foreach ($uc as $key => $value) {
        $user = get_userdata($key);
        $author_posts_url = get_author_posts_url($key);
        $post_count = $value;
        echo 'User ID ' . $user->ID . ' ' . $user->user_firstname . ' ' . $user->user_lastname . ' number of posts:' . $post_count . ' author posts url:' . $author_posts_url .'';
      }
    }
    ?>

    [Edit - note that it is descending using arsort]

  3. igneous
    Member
    Posted 5 years ago #

    couldnt get that to work, does anyone else have any other thoughts?

  4. mpiftex
    Member
    Posted 5 years ago #

    MichaelH you're a life saver!! The code above works GREAT!! I've been searching for a way to do this for hours. Have you figured out by any chance if the code is efficient? Are there many db queries taking place? (I have no idea, just asking). Again, many thanks.

    igneous, give it another try. Just copy paste it into your code and then change the echo line to show the user info! You can use this list to get the user information (name, url, etc) you need.

  5. djdiego88
    Member
    Posted 4 years ago #

    Hi MichaelH
    Forgive my English
    The code works for me, but I need only show 10 or 15 authors by the amount of post they made.
    How can I restrict that the code only show me 10 or 15 authors ordered by $post_count?

    Thanks

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Looks ok to me..
    Average time, 100 iterations (just to benchmark)..

    0.00684

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags