WordPress.org

Ready to get started?Download WordPress

Forums

List posts only from authors level? (9 posts)

  1. wasferraz
    Member
    Posted 3 years ago #

    Hi there, please guys, I need to finalize an job, list the last 2 posts from authors, only from authors level.

    This list it will display the author avatar, the autor name, the post title and the date of the post title.

    The HTML structure is below:

    <ul>
     <li>
     <img src="AVATAR"/>
     <h6>[author name] at [post date]</h6>
     <a href="#" title="rrrr">[post title]</a>
     </li>
    </ul>

    Help-me guys!

  2. MichaelH
    Member
    Posted 3 years ago #

    So you are saying, display a list of users that are of the Author role and display two posts for each of those Authors--correct?

    What if they are of the Author role and don't have any posts?

  3. wasferraz
    Member
    Posted 3 years ago #

    Hi MichaelH, no, I need list the last 2 posts from authors role, ordered by date in my homepage. But I need only authors role. Excluding from this list, posts by Admins and others roles.

  4. MichaelH
    Member
    Posted 3 years ago #

    <?php
    //displays users of author role and two posts for each
    $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('author',$user->roles) ) ){
          //echo "<pre>"; print_r($user); echo "</pre>";
          echo '<p>User ID ' . $user->data->ID . ' ' . $user->data->user_firstname . ' ' . $user->data->user_lastname . '</p>';
    
          $args=array(
            'author' => $user->data->ID,
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 2,
            'caller_get_posts'=> 1
          );
          $my_query = null;
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
            echo 'List of Posts for this user';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            endwhile;
          }
          wp_reset_query();  // Restore global post data stomped by the_post().
        }
      }
    }
    ?>
  5. wasferraz
    Member
    Posted 3 years ago #

    Thanks for the reply, but, in case of the code above it defines one author and list 2 posts of him.

    What I need is list the last 2 posts posted by author's role, and not the last 2 posts of one only author.

    See my website in the link below:
    http://www.informecidade.com/portal

    In the section "Artigos Mais Recentes de Counistas" - "Most Recente Articles from Columnists" I want to list the most recente posts from my columnists, that were registered at admin like "Authors".

    The rest content of my website are posted by me, and I am the Admin of it. This is the why I don't need my posts listed in that section, becouse that section is only to list the Columnists "Authors" posts.

    Is now easier to understand what I need?

    Sorry if I not explained better before!

  6. MichaelH
    Member
    Posted 3 years ago #

    Well that code lists each user of the Author Role and two posts (if there are two posts).

    Oh well maybe someone else can jump in here with a better solution. Good Luck.

  7. wasferraz
    Member
    Posted 3 years ago #

    MichaelH, in your code how can I limit to show only 2 posts a time?

    Becouse in your code, if I have 10 Authors, it shows the 10 authors last post. I need to show Only the last 2 independing the total of authors.

    Can you help me?

  8. MichaelH
    Member
    Posted 3 years ago #

    Then you'd need to capture the ID of the author users in an array that are 'authors', then do a query posts loop and check to see if the author of each post was part of your author array.

  9. wasferraz
    Member
    Posted 3 years ago #

    I can't do your last reply, help me!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags