• Resolved scdwb

    (@scdwb)


    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.

Viewing 14 replies - 1 through 14 (of 14 total)
  • 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.

    Thread Starter scdwb

    (@scdwb)

    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 🙂

    Thread Starter scdwb

    (@scdwb)

    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

    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>';
      }
    }
    ?>

    Thread Starter scdwb

    (@scdwb)

    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!

    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>';
    }

    Thread Starter scdwb

    (@scdwb)

    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!

    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);
    }
    ?>

    Thread Starter scdwb

    (@scdwb)

    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 😀

    Thank you for this!

    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?

    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>';
      }
    ?>

    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.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Display number of authors in a category’ is closed to new replies.