Forums

List authors from category by date of latest post with titles (2 posts)

  1. duskom
    Member
    Posted 6 months ago #

    Hello,

    So i need to list 3 authors from category "blogeri" by date of latest posts with author's avatar and title of respective latest post that should be linked to the story.

    Below is compiled code that I have found here that do everything I need except the post title part. This snippet shows number of total posts of an author. What I need is to show latest post title of the respective author.

    <ul>
    <?php
    //List of users sorted descending by date of lastest post written by user
    $uc=array();
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = new WP_User( $bloguser->user_id );
        if ( !empty( $user->roles ) && is_array( $user->roles ) && ( in_array('blogeri',$user->roles) ) ){
        $userpost = get_posts('showposts=1&author='.$bloguser->user_id);
        $uc[$bloguser->user_id] = '';
        if ($userpost) {
          $uc[$bloguser->user_id]=$userpost[0]->post_date;
    	}
    }
      }
      arsort($uc);
      $i = 0;
      foreach ($uc as $key => $value) {
        $user = get_userdata($key);
        $post_count = get_usernumposts($user->ID);
        if ($post_count && $i < 3) {
          $author_posts_url = get_author_posts_url($key);
          echo '<p class="classul">' . get_avatar($user->ID, 46). '<a href="' . $author_posts_url . '">' . $user->user_firstname . ' ' . $user->user_lastname . '</a> (' . $post_count . ')</p>';
        }
      $i++;
    }
    }
    ?>
    </ul>
  2. duskom
    Member
    Posted 6 months ago #

    Solved by mixing yet another code from Michael. You can see results here

    <ul>
    <?php
    //List of users sorted descending by date of lastest post written by user
    $uc=array();
    $blogusers = get_users_of_blog();
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $user = new WP_User( $bloguser->user_id );
        if ( !empty( $user->roles ) && is_array( $user->roles ) && ( in_array('blogeri',$user->roles) ) ){
        $userpost = get_posts('showposts=1&author='.$bloguser->user_id);
        $uc[$bloguser->user_id] = '';
        if ($userpost) {
          $uc[$bloguser->user_id]=$userpost[0]->post_date;
    	}
    }
      }
      arsort($uc);
      $i = 0;
      foreach ($uc as $key => $value) {
        $user = get_userdata($key);
        $post_count = get_usernumposts($user->ID);
        if ($post_count && $i < 3) {
          $author_posts_url = get_author_posts_url($key);
    
    //preparing query for titles
    	  $args=array(
          'author' => $user->ID,
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 1,
          'caller_get_posts'=> 1
        );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          //echo 'List of Posts for ' . user->user_firstname . ' ' . $user->user_lastname;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
           <?
    	    echo '<p class="classul"><div style="float:left; margin-right:5px">' . get_avatar($user->ID, 46). '</div><a href="' . $author_posts_url . '"><b>Autor:</b> ' . $user->user_firstname . ' ' . $user->user_lastname . '</a><br>'; ?>
           <b><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></b><br><br></p>
            <?php
          endwhile;
        }
      wp_reset_query();
        }
      $i++;
    }
    }
    ?>
    </ul>

Reply

You must log in to post.

About this Topic