WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. andersedvardsen
    Member
    Posted 1 year ago #

    Hello,

    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?

    Anders

    <div class="authorsContainer">
    
                <?php
                    //define vars from user selection
                    $howManyAuthors = 50;
    
                    //Get users and count of posts put into array
                    $authorsArray=array();
                    $blogusers = get_users_of_blog();
                    if ($blogusers) {
                      foreach ($blogusers as $bloguser) {
                        $post_count = get_usernumposts($bloguser->user_id);
                        $authorsArray[$bloguser->user_id]=$post_count;
                      }
                      arsort($authorsArray);
                      $maxauthor = $howManyAuthors;
                      $count=0;
                      foreach ($authorsArray as $key => $value) {
                      $count++;
                        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>
                            </div>
                            <ul>
                            <?php
                              $args=array(
                                'showposts'=>1,
                                'author'=>$user->ID,
                                'caller_get_posts'=>1
                              );
                              $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>  
    
                          <?php
                                endwhile;
                             }
                          ?>
                          </ul>
                        </div><!-- end of topWriterBox -->
                        <?php
                    }
                  }
                }
                ?>
    
            </div>
  2. vjpo
    Member
    Posted 1 year 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();
    to
    $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