Only include author-role in my user-listing script (2 posts)

  1. andersedvardsen
    Posted 2 years ago #


    The snippet at the end of this post is perfect for what i want, except one thing: it includes all roles. So this is what i am doing. I am listing all the authors with a number displaying how many post they have published, and a link to their latest post. Their name links to an overview over all the posts. The problem is that i would like to display ONLY one role, the author-role, but currently every user in wordpress get listed (admins, editors, authors, subscribers). How can i exclude the roles i do not want?


    <div class="authorsContainer">
                    //define vars from user selection
                    $howManyAuthors = 50;
                    //Get users and count of posts put into array
                    $blogusers = get_users_of_blog();
                    if ($blogusers) {
                      foreach ($blogusers as $bloguser) {
                        $post_count = get_usernumposts($bloguser->user_id);
                      $maxauthor = $howManyAuthors;
                      foreach ($authorsArray as $key => $value) {
                        if ($count <= $maxauthor) {
                            $user = get_userdata($key);
                            $username = $user->user_login;
                            $displayname = $user->display_name;
                            $author_posts_url = get_author_posts_url($key);
                            $post_count = $value;
                        <div class="topWriterBox">
                            <div class="topWriterName">
      <div class="topWriterArticles"><?php echo __('', '').$post_count.__('', ''); ?></div>                            
    <a href="<?php echo $author_posts_url; ?>"><?php echo $displayname; ?> </a>
                              $my_query = new WP_Query($args);
                              if( $my_query->have_posts() ) {
                                while ($my_query->have_posts()) : $my_query->the_post();
                            <!--============ THE AUTHORS ARTICLE DATA ================-->
                                <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permenent Link To <?php the_title_attribute(); ?>"><?php the_title(); ?></a> <small>(<?php the_time('m.d.y') ?></small>)</li>  
                        </div><!-- end of topWriterBox -->
  2. vjpo
    Posted 2 years ago #

    It is pretty easy if to use get_users() instead of deprecated get_users_of_blog().
    So, you can filter users by role if to change
    $blogusers = get_users_of_blog();
    $blogusers = get_users('role=author');

    $bloguser->user_id in this case must be changed to $bloguser->ID
    (Check right code for other values of a user array here)

    It would be better also to use count_user_posts() instead of deprecated function get_usernumposts().

Topic Closed

This topic has been closed to new replies.

About this Topic