Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a website with many authors and posts. I need to create a list of all authors and for each one display his latest post (not posts, only one post).

This better explains what I need:

<ul>
    <li>
        <h2>name of the author</h2>
        <a href="his latest post"> Title of his latest post </a>
    </li>
</ul>

This is the code I am using so far which works OK but it's only one loop. I need to create many of these if I want to display all authors automatically:

<ul>
    <?php
    query_posts('showposts=1&author=7'); 
    if (have_posts()) : while (have_posts()) : the_post();
    ?>
        <li>
            <h2>name of the author</h2>
            <a href="his latest post"> Title of his latest post </a>
        </li>
    <?php
    endwhile; endif; 
    ?>
</ul>

Is there any simple way of doing it with only one loop?

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

In @Sagive SEO answer get_users_of_blog() is a depricated function.

What is below code do

  1. get all authors display name and id in $authorsList
  2. Loop threw each author id and use WP_Query to get latest post of each author

       <?php
        $authors=get_users();
        $i=0;
        //get all users list
        foreach($authors as $author){
            $authorList[$i]['id']=$author->data->ID;
            $authorList[$i]['name']=$author->data->display_name;
            $i++;
        }
        ?>
        <ul>
            <?php 
            foreach($authorList as $author){
                $args=array(
                        'showposts'=>1,
                        'author'=>$author['id'],
                        'caller_get_posts'=>1
                       );
                $query = new WP_Query($args);
                if($query->have_posts() ) {
                    while ($query->have_posts()){
                        $query->the_post();
            ?>
            <li>
             <h2><?php echo $author['name']; ?></h2>
             <a href="<?php echo get_permalink(); ?>"> <?php echo get_the_title(); ?> </a>
            </li>
            <?php
                    }
                    wp_reset_postdata();
                }
            }
            ?>
        </ul>
    

Important Link:

get_users()

share|improve this answer
    
Thanks @Ravs. I needed it long time ago and it works but I will definitely check your solution in a future project –  gil hamer Jun 5 '13 at 20:43
add comment

i have something i already crated for this purpose so you need to change it
if you don't like how its displayed... anyhow:

In My Opinion you need to:
1. Grab all the authors (into an array)
2. Run trough that array while building the list
3. Kick back and enjoy the results ;)

Please Note...
I think its a bad idea to show all writers in one page
because it might take a lot of time to load so you can
set the amount of authors you wish to show by changing
the variable: $howManyAuthors

Someone else is due credit for the original code but its
long ago since i took it and customized it... if your
checking this out mate - thanks

        <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">
                            <a href="<?php echo $author_posts_url; ?>"><?php echo $displayname; ?> </a>
                        </div>
                        <div class="topWriterArticles"><?php echo __('Published: ', '').$post_count.__(' Articles', ''); ?></div>
                        <br class="clr" />

                        <h4><?php _e('Latest Article:', ''); ?></h4>

                        <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>

.

You can see an example here (its in hebrew <- not an issue, right gil?

Hope this helps,
Cheers, Sagive.

share|improve this answer
    
Wow. thanks Sagive! I will test it tomorrow and let you know how it went.. –  gil hamer Aug 25 '12 at 21:21
    
Working like charm Sagie! Great solution!! Thanks –  gil hamer Aug 26 '12 at 6:29
    
gr8 - glad its working for you mate. –  Sagive SEO Aug 26 '12 at 7:31
    
Hi @Sagive SEO, back to you and your awesome script. is there a way to display the authors randomly and not by posts count? Thanks, Gil –  gil hamer Jan 2 '13 at 8:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.