WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Latest 5 authors sorted by posting date (21 posts)

  1. frivolio
    Member
    Posted 3 years ago #

    Hi everybody!

    I need to show the latest 5 authors (actually contributors in WordPress role management) order by their latest data posts.

    I mean I have around 20 authors and I need to display only 5 authors who posted most recently!

    Anybody can help me with this?

    Thanks a lot!

  2. devilson
    Member
    Posted 3 years ago #

    http://net.tutsplus.com/tutorials/wordpress/quick-tip-popular-posts-by-comment-count-sql-query-in-wordpress/

    Its not what you want but select different parts of the DB should work.

  3. frivolio
    Member
    Posted 3 years ago #

    Thanks a lot devilson. I studied your link, but I’m afraid my expertise is not high enough to complete this task without more in deep explanations. :(

  4. MichaelH
    Member
    Posted 3 years ago #

    but I’m afraid my expertise is not high enough to complete this task without more in deep explanations.

    Where do you want to display that information--in your sidebar? If so does your theme support widgets? If necessary can you install a plugin?

  5. frivolio
    Member
    Posted 3 years ago #

    Yes I can install/use a plugin or a sidebar widget.

  6. devilson
    Member
    Posted 3 years ago #

    if you dont want it in the side bar im willing to put it to gether for you.

  7. frivolio
    Member
    Posted 3 years ago #

    Thank you devilson! I will highly apreciate if you can help me with this!

  8. devilson
    Member
    Posted 3 years ago #

    <?php $pop = $wpdb->get_results("SELECT id, post_author, guid FROM {$wpdb->prefix}posts WHERE post_type='post' ORDER BY id DESC LIMIT 5");
    foreach($pop as $post) : ?>
    <?php $pop1 = $wpdb->get_results("SELECT user_nicename FROM {$wpdb->prefix}users");
    foreach($pop1 as $user) : ?>
    
    <li><a href="<?php echo $post->guid; ?>"><?php echo $user->user_nicename; ?></a></li>
    
    <?php endforeach; ?><?php endforeach; ?>

    That should work put it where you want it.

    It could be shorter but im still learning php.

    Edit: If it doesn't work tell me as i built it on my local host with one user.

  9. frivolio
    Member
    Posted 3 years ago #

    Hi devilson! Thanks a lot. I tested but I’m afraid it doesn't work as it should. The code displays all my users for 5 times. :(

  10. devilson
    Member
    Posted 3 years ago #

    Hi devilson! Thanks a lot. I tested but I’m afraid it doesn't work as it should. The code displays all my users for 5 times. :(

    Ill look into it.

  11. devilson
    Member
    Posted 3 years ago #

    I found this but i doesn't order them and im not sure how to limit the ammount it shows.

    <?php list_authors(TRUE, TRUE); ?>

  12. frivolio
    Member
    Posted 3 years ago #

    Unfortunately function list_authors will not help me in any way! :(

  13. devilson
    Member
    Posted 3 years ago #

    In that case i would use a plugin.

  14. MichaelH
    Member
    Posted 3 years ago #

    <?php
    //list 5 latest authors
    $authors =  array();
    $count = 0;
    $args=array(
      '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 'Latest 5 authors';
      while ($my_query->have_posts() && $count < 5) : $my_query->the_post();
        $author_id=$my_query->post->post_author;
        if (!in_array($author_id,$authors)) {
          echo '<p>';
          the_author_posts_link();
          echo '</p>';
        $count++;
        $authors[]=$author_id;
        }
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  15. frivolio
    Member
    Posted 3 years ago #

    Thanks a lot! The code look nice but when I tried to executed it I run into this:

    Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 32768 bytes) in ... /wp-includes/wp-db.php on line 734

  16. MichaelH
    Member
    Posted 3 years ago #

    Methods for increasing the amount of memory a PHP script may consume

    1. If you have access to your PHP.ini file, change the line in PHP.ini
    If your line shows 32M try 64M:
    memory_limit = 64M ; Maximum amount of memory a script may consume (64MB)

    2. If you don't have access to PHP.ini try adding this to an .htaccess file:
    php_value memory_limit 64M

    3. Try adding this line to your wp-config.php file:
    define('WP_MEMORY_LIMIT', '64M');

    4. Talk to your host.

  17. frivolio
    Member
    Posted 3 years ago #

    I tried with define('WP_MEMORY_LIMIT', '64M')
    and it works! :) Let me test it and I will also try to change it for contributors type of users. I will come back with my conclusions! Thanks a lot!

  18. MichaelH
    Member
    Posted 3 years ago #

    Oops forgot your 'contributor' restriction:

    <?php
    //list 5 latest contributors
    $authors =  array();
    $count = 0;
    $args=array(
      '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 'Latest 5 contributors';
      while ($my_query->have_posts() && $count < 5) : $my_query->the_post();
        $author_id=$my_query->post->post_author;
        $user = new WP_User( $author_id );
        if ( !empty( $user->roles ) && is_array( $user->roles ) && in_array('contributor',$user->roles) ) {
          if (!in_array($author_id,$authors)) {
            echo '<p>';
            the_author_posts_link();
            echo '</p>';
            $count++;
            $authors[]=$author_id;
          }
        }
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  19. frivolio
    Member
    Posted 3 years ago #

    It works like a charm!!! Thanks a lot MichaelH! You're the man! Thank you!

  20. devilson
    Member
    Posted 3 years ago #

    Thank you, MichaelH i can learn allot from that.

  21. chavo
    Member
    Posted 2 years ago #

    Michael...you rocks man!!! Thank you a lot, really! I came from this topic http://wordpress.org/support/topic/show-latest-posts-by-all-authors and then I found this solution. I rewrite the code to show some extra info. Here is the code:

    <?php
    //list 6 latest contributors
    $authors =  array();
    $count = 0;
    $args=array(
      '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 'Latest 6 contributors';
      while ($my_query->have_posts() && $count < 6) : $my_query->the_post();
        $author_id=$my_query->post->post_author;
        $user = new WP_User( $author_id );
        if ( !empty( $user->roles ) && is_array( $user->roles ) && !in_array('administrator',$user->roles)) {
          if (!in_array($author_id,$authors)) { ?>
            <?php echo '<p>'; ?>
            <a href="<?php echo $user_link; ?>" title="<?php echo $user->display_name; ?>"><?php echo get_avatar( $user->ID, 80, 'http://www.naciondeaccion.com/v1/wp-content/themes/default/images/ndaAvatar.gif' ); ?></a>
    		          <?php if($user->deporte) { ?>
              <?php echo $user->deporte; ?>
              <?php } ?>
    		<?php the_author_posts_link(); ?>
            <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
            <?php echo '</p>'; ?>
    
    		<?php $count++;
            $authors[]=$author_id;
          }
        }
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    I limit to display no administrator users with !in_array('administrator',$user->roles) but I can figure out how to display only editors and contributors. How can I do it?

    Once again thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic