WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display number of authors in a category (15 posts)

  1. scdwb
    Member
    Posted 4 years ago #

    Hi all,

    I would like to display the number of authors in my chosen category. I have a website with the category Cities. Each post is a property listing and these are added by the authors, which are letting agents.

    So when a user is in the London category I would like to display something like:

    "There are 14 letting agents active in London".

    Looking at the funciton reference I can only find it to display the authors with wp_list_authors. It would be good if I could use something like wp_list_authors(cat_ID=50)->count. But this isn't supported.

    Thanks for your help guys.

  2. MichaelH
    Member
    Posted 4 years ago #

    You'll need to use query_posts/get_posts to retreive the posts in your given category, then loop through those posts and store the list of authors encountered in an array. Then iterate through that array to display the authors.

  3. scdwb
    Member
    Posted 4 years ago #

    Hi Michael, thanks again. I don't know if you remember but this is going to be another method to solve my other problem we had no luck with before.

    Ok, hmm im new to php. So can you help me out? Based on what you say im thinking something like:

    query_posts("cat=$city&showposts=-1");
    $authors = wp_list_authors();
    
      foreach($authors as $author) {
        $number= $author->count;
      }
    }
    echo 'Number of authors is '.$number;

    This obviously doesn't work but im trying :-)

  4. MichaelH
    Member
    Posted 4 years ago #

  5. scdwb
    Member
    Posted 4 years ago #

    Its hard for a beginner to know how to use the arrays with the wordpress functions. I am familiar with arrays but how do I store the lists of authors in an array?

    Is the foreach statement in my code correct? How do I store the list of authors encountered in an array?

    Please help

  6. MichaelH
    Member
    Posted 4 years ago #

    One possibility:

    <?php
    //get all posts in a given category, then create array of all authors
    //of those posts, then sort the array in reverse order by number
    //of posts, then display those authors
    $catauthors = array();
    $citycat=1;
    $allposts=get_posts("cat=$citycat&showposts=-1");
    if ($allposts) {
      foreach($allposts as $authorpost) {
        $catauthors[$authorpost->post_author]+=1;
      }
      arsort($catauthors); //sort array in reverse order by number of posts
      foreach($catauthors as $key => $author_post_count) {
        $curuser = get_userdata($key);
        $author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
        echo '<p>--User nicename: '.$curuser->user_nicename .', display name: '. $curuser->display_name . ', number of posts in category '.$citycat.': ' .$author_post_count .', link to all posts by this author:  <a href="' . $author_post_url . '" title="' . sprintf( __( "Posts by %s" ), $curuser->user_nicename ) . '" ' . '>' . $curuser->user_nicename .'</a></p>';
      }
    }
    ?>
  7. scdwb
    Member
    Posted 4 years ago #

    This is getting there. It listed the authors details in the $citycat category and echos out the details of the authors. But all I need it to do is echo out the number of authors from the &citycat category?

    Im a bit lost of how to make this work using the code above.

    Thanks in advance!

  8. MichaelH
    Member
    Posted 4 years ago #

    After this:

    foreach($allposts as $authorpost) {
        $catauthors[$authorpost->post_author]+=1;
      }

    put

    echo 'count of authors is '.count($catauthors);

    And then delete these lines:

    arsort($catauthors); //sort array in reverse order by number of posts
    foreach($catauthors as $key => $author_post_count) {
      $curuser = get_userdata($key);
      $author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
      echo '<p>--User nicename: '.$curuser->user_nicename .', display name: '. $curuser->display_name . ', number of posts in category '.$citycat.': ' .$author_post_count .', link to all posts by this author:  <a href="' . $author_post_url . '" title="' . sprintf( __( "Posts by %s" ), $curuser->user_nicename ) . '" ' . '>' . $curuser->user_nicename .'</a></p>';
    }
  9. scdwb
    Member
    Posted 4 years ago #

    Im getting somewhere now but it has some problem. I have this code:

    $catauthors = array();
    $allposts=get_posts("cat=$city&showposts=-1");
    if ($allposts) {
      foreach($allposts as $authorpost) {
        $catauthors[$authorpost->post_author]=1;
    	echo count($catauthors);
      }}

    But if there is 1 instance of 4 different authors it echoes 1234.

    If there are 2 instances of 4 different authors it echoes 11223344.

    I need it to just echo the number of different authors in the categories. So if 1 author posted 100 posts and the other 3 just posted 1 in this category it would just display 4.

    This must be possible? Thank you!

  10. MichaelH
    Member
    Posted 4 years ago #

    Sorry should have provided all the code, as you've got the count() in the wrong place:

    <?php
    //get all posts in a given category, then create array of all authors used on those posts,
    //the display a count of the authors
    $catauthors = array();
    $citycat=1;
    $allposts=get_posts("cat=$citycat&showposts=-1");
    if ($allposts) {
      foreach($allposts as $authorpost) {
        $catauthors[$authorpost->post_author]+=1;
      }
      echo 'count of authors is '.count($catauthors);
    }
    ?>
  11. scdwb
    Member
    Posted 4 years ago #

    Wahoo! Thank you it is finally working. This is my finished code:

    $catauthors = array();
    $allposts=get_posts("cat=$city&showposts=-1");
    if ($allposts) {
      foreach($allposts as $authorpost) {
        $catauthors[$authorpost->post_author]=1;
      }
      echo count($catauthors);
    }

    Thanks Michael, I have learned a lot from this :-D

  12. ingero
    Member
    Posted 4 years ago #

    Thank you for this!

  13. Golok
    Member
    Posted 4 years ago #

    Excellent hackā€¦! Thanks a lot!

    Regarding your first piece of code, is there any way to sort authors by name rather than by number of posts?

  14. MichaelH
    Member
    Posted 4 years ago #

    From my notes:

    List user sorted by last name:
    <?php
    $lastnames = $wpdb->get_col("SELECT user_id FROM $wpdb->usermeta WHERE $wpdb->usermeta.meta_key = 'last_name' ORDER BY $wpdb->usermeta.meta_value ASC");
      foreach ($lastnames as $userid) {
        $user = get_userdata($userid);
        $post_count = get_usernumposts($user->ID);
        $author_posts_url = get_author_posts_url($user->ID);
        echo '<li><a href="' . $author_posts_url . '">' . $user->user_firstname . ' ' . $user->user_lastname . '</a> (' . $post_count . ') </li>';
      }
    ?>
  15. Golok
    Member
    Posted 4 years ago #

    Thanks thousand times. Your code lists indeed all users and their number of posts.

    Unfortunately, I have no clue how to join the two, i.e. displaying authors of a given category in alphabetical order. I tried different things but to no avail and my little knowledge of php gives me no hope.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags