• Hello — I’m trying to show a list of the authors on my site, sorted by recent post date. I’m 99% there, because I am showing the author name, biography, and recent post just as I want. I just can’t get the sorting to work properly, because what I think I want is ‘orderby’ => ‘post_date’, but that’s obviously something I made up that doesn’t exist.

    Can this be done? My code is here:

    <?php
    /**
     * Template Name: All Author Archives
     */
    
    get_header(); get_sidebar();?>
        <div id="all-authors-archive">
          <h1 style="font-family: 'Noto Sans', sans-serif;"><?php the_title(); ?></h1>
    <?php
    // Arguments to pass to get_users
    $args = array( 'orderby' => 'display_name', 'order' => 'ASC', 'who' => 'authors', 'exclude' => '1, 31052', 'post_type' => 'post', 'post_status' => 'publish' );
    // Query for the users
    $authors = get_users( $args ); ?>
    
    <?php
    // Loop through all the users, printing all of their posts as we go
    foreach ( $authors as $author ) { ?>
          <a name="<?php echo $author->user_nicename; ?>"></a>
          <div class="author-posts-wrapper" id="author-<?php echo $author->ID; ?>-posts-wrapper">
           <div class="author-posts" id="author-<?php echo $author->ID; ?>-posts">
              <div id="author-page-name"><a href="<?php echo get_author_posts_url( $author->ID ); ?>"><?php echo $author->display_name; ?></a><br>
               <img src="/tht/wp-content/images/author_twitter_icon.jpg"><span id="author_twitter">twitter here</span></div>
              <div id="author_bio"><?php echo $author->description; ?></div><br>
     <div id="authors_recently"><em>Recently: </em>&nbsp; <?php
      // Set up a Loop, querying for all of the current user's posts
      $args = array( 'author' => $author->ID, 'posts_per_page' => 1 );
      $posts = query_posts($args);
      // Now that we have the posts, simulate a Loop here or use get_template_part
      // if we already have the output in another template, like:
      // get_template_part( 'loop', 'all-authors' ); // Pulls in loop-all-authors.php from theme
      if ( have_posts() ) : ?>
           <?php while ( have_posts() ) : the_post(); // Print whatever we want for each post - for now the title and date ?>
             <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> &mdash; <?php echo get_the_date(); ?>
        <?php endwhile; ?>
            </div><!-- #author-post-list -->
      <?php else: ?>
              <p>This author has not yet published any posts</p>
      <?php endif; ?>
            </div><!-- #author-posts -->
          </div><!-- #author-posts-wrapper -->
    <br><hr><?php } // End looping over all users ?>
    
        </div><!-- #all-authors-archive -->
    <?php
    get_footer();
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Do you want to sort the authors by whoever has most recently posted, or just get the most recent post for each individual author?

    Thread Starter mpet29

    (@mpet29)

    The former. I already have it spitting out each author’s most recent post. I just want that block — author name, bio, recent post — sorted by the author with most recent post at the top, and that’s what I’m having trouble with.

    If i’m reading your code right, it follows the following logic:

    1). Use get_users() to return a list of author objects.
    2). A foreach loop to cycle through each of the authors individually.
    3). Set up divs that pull out all of the author’s metadata and display it.
    4). Use query_posts() to get and then display a list of said author’s most recent posts.

    Looking at the codex for get_users(), it seems that there is no way, using that function, to achieve what you want. You CAN achieve something like this using new wp_query() by passing ‘orderby’=>’ author date’ in the parameters (you can pass two values to the orderby parameter). Given this, you might think of doing is reorganizing your logic as follows:

    1). Call a new wp_query and loop through all the posts it returns.
    2). Within the while loop, create a new wp_query based on the data returned to you via the first query.This should include the author id of the first returned post, and an array of author names/ids to exclude from consideration. This should to return a loop focused only on the author of the MOST RECENT post, and will exclude duplicate author records in the list.
    3). Use the data returned in this loop to echo out the author’s meta data, and a list of their most recent posts (‘orderby’=>’date’).
    4). Add the author’s name to an array of author’s names/ids that is appended every time you iterate through the loop. This array is passed every time to the second loop (in step 2), as a list of authors to exclude from consideration. This should exclude records of duplicate authors.

    I didn’t give you an example of code because I want to make sure this is what you’re looking to achieve.

    Thread Starter mpet29

    (@mpet29)

    Right. Right now, it’s something like this:

    ANDY
    bio information about ANDY
    Recent ANDY post: Jan 18, 2014

    BILLY
    bio information about Billy
    Recent BILLY post: Jan 20, 2014

    What I need is for Billy’s section to appear first, since his latest post came most recently.

    I am admittedly not very strong at PHP, so if what I created is not the most efficient way to do this, that would not surprise me at all.

    Ok, this is totally untested, but I believe it encapsulates the idea I was trying to put forth. It’s probably not going to work without serious surgery, so I would NOT just dump it into your theme. Backup your file before you try it out, if you do at all.

    <?php
    $excludeauthor = array();
    //FIRST LOOP - TO GET LIST OF RECENT POSTS
    $post_query = new WP_Query($args); ?>
    <?php if ( $post_query->have_posts() ) : ?>
    <?php while ( $post_query->have_posts() ) : $post_query->the_post();
    
    //declare author array as global
    global $excludeauthor;
    $present_author = array($post->post_author);
    
    //Arguments for the second loop that will make a loop based on the
    //author of the most recent post(s).
    $args=array(
    	'orderby' => 'date',
    	'post_status' => 'publish',
    	'posts_per_page' => -1,
    	'author__in' => $present_author,
    	'author__not_in' => $excludeauthor
    );
    
    	//SECOND LOOP - Queries specific author of most recent post
    	$author_query = new WP_Query($args); ?>
    	<?php if ( $author_query->have_posts() ) : ?>
    	<?php while ( $author_query->have_posts() ) : $author_query->the_post(); ?>
    	<?php global $present_author; ?>
    
    		<?php //Get all the information regarding the present author as an object ?>
    		<?php $author = get_user($present_author); ?>
    
    		<?php //Create all the html output for the author and post ?>
    		<a name="<?php echo $author['user_nicename']; ?>">
    		</a>
          	<div class="author-posts-wrapper" id="author-<?php echo $author['ID']; ?>-posts-wrapper">
           	<div class="author-posts" id="author-<?php echo $author['ID']; ?>-posts">
              <div id="author-page-name">
    		<a href="<?php echo get_author_posts_url( $author['ID'] ); ?>">
    		<?php echo $author['display_name']; ?>
    		</a>
    		<br>
               <img src="/tht/wp-content/images/author_twitter_icon.jpg">
    		<span id="author_twitter">twitter here</span></div>
              <div id="author_bio">
    		<?php echo $author['description']; ?>
    		</div>
    		<br>
     		<div id="authors_recently">
    		<em>Recently:</em>&nbsp;
    
    		<?php //OUTPUT THE ACTUAL HTML PART OF THE LOOP FEATURING THE AUTHOR'S MOST RECENT POST(S) ?>
    		<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    		<?php the_title(); ?>
    		</a>
    		&mdash;
    		<?php echo get_the_date(); ?>
        		<?php endwhile; ?>
            	</div><!-- #author-post-list -->
      		<?php else: ?>
             		<p>This author has not yet published any posts</p>
     		<?php endif; ?>
            	</div><!-- #author-posts -->
          	</div><!-- #author-posts-wrapper -->
    		<br><hr>
    
    	<?php //ENDS SECOND LOOP ?>
    	<?php else:  ?>
     	 <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>
    
    <?php
    //When the second loop ends, add the present author to a
    //list of authors to exclude from later loops, but ONLY if
    //that author is not already in the list of authors to exclude.
    if (!in_array($present_author, $exclude_author)){
    array_push($present_author,$excludeauthor);
    }
    
    //ENDS FIRST LOOP?>
    <?php wp_reset_postdata(); ?>
    <?php else:  ?>
      <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Sort authors list by recent post’ is closed to new replies.