• Resolved cezar

    (@cezar)


    Hi everyone. I have stumbled into a problem i can’t seem to figure out. Not even sure if possible.
    I am trying to display an authors list, ordered by nickname but also include the latest post by each user.
    so i have this code:

    <?php
         $excluded = "0,1";  // To exclude IDs 1
         $sql = 'SELECT DISTINCT post_author FROM '.$wpdb->posts. " WHERE post_author NOT IN ($excluded)";
         $authors = $wpdb->get_results($sql);
    	if($authors):
    	foreach($authors as $author):
    ?>
        <div class='authors' id='author-<?php the_author_meta('user_login', $author->post_author); ?>'>
    	<h3><a href="<?php bloginfo('url'); ?>/author/<?php the_author_meta('user_login', $author->post_author); ?>"><?php the_author_meta('display_name', $author->post_author); ?></a></h3>
    	<?php
    	$recentPost = new WP_Query('author='.$author->post_author.'&showposts=1');
    	while($recentPost->have_posts()): $recentPost->the_post();
    	?>
    	    <h4>Has recently published: <a href="<?php echo get_permalink() ?>"><?php the_title(); ?></a></h4>
    	<?php endwhile; ?>
         </div>
    <?php endforeach; endif; ?>

    this works except it orderes by ID.
    any help on this would me much appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think this query will work:

    $sql = "SELECT DISTINCT post_author, meta_value as nickname
       FROM $wpdb->posts
       JOIN $wpdb->usermeta ON (post_author = user_id)
       WHERE post_type = 'post'
       AND post_status = 'publish'
       AND user_id NOT IN ($excluded)
       AND meta_key = 'nickname'
       ORDER BY UPPER(nickname)";
    Thread Starter cezar

    (@cezar)

    Yes yes yes.
    you’re THE mysql query man.
    simple, pretty, effective.
    thanks!
    Cezar

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘authors list with latest post ordered by nickname’ is closed to new replies.