WordPress.org

Ready to get started?Download WordPress

Forums

A function to call 10 latest post with no repeated author (7 posts)

  1. williamchan04
    Member
    Posted 1 year ago #

    Hello there~ Thank you for clicking in first.
    I am trying to create a list that contains the latest post from different authors & one specific category on the widget area. WordPress default to have the latest post widget, but if one of the authors writes a few posts the roll, he/she takes all the spots on the latest post widget. To be simply, I write down the requirement in points:

    -10 latest posts
    -no repeated author
    -specific category

    So ten posts should be all from different authors and sort by date.

    This is my attempt, please point out what I am doing wrong =D Newbie in programming

    $args = array(
        'order' => 'DESC',
        'orderby' => 'date',
        'cat' => 39
        );
    $query = new WP_Query($args);
    $final= array_fill(0, 10, array('',''));
    
    if( $query->have_posts() ) {
        while ($query->have_posts()) {
        	 for($i=0; $i < 10; $i++ ){
        	 $query->the_post();
             $authors = get_the_author_meta('ID');
    
             if ($final[$i][0] != $authors){
             $final[$i][1] = '
    <li><a href="'.get_permalink().'">'.get_the_title().'</a></li>
    ';
    
             }
             }
             }
    
           }
    
        print_r ($final);
  2. Timothy Jacobs
    Member
    Posted 1 year ago #

    please edit your question and put the code with back ticks ie this `, or just hit the code button

  3. williamchan04
    Member
    Posted 1 year ago #

    thankyou for pointing it out

  4. Timothy Jacobs
    Member
    Posted 1 year ago #

    If there aren't enough posts do you want to let an author have more than one post, or would you rather just stop even if that means having less than 10 posts?

  5. Timothy Jacobs
    Member
    Posted 1 year ago #

    Try this, if there are less than 10 authors it will just stop displaying posts, so it may display a list of less than 10.

    $args = array(
    	'order'         => 'DESC',
    	'orderby'       => 'date',
    	'cat'           => 39,
    	'numberposts'   => -1
    );
    
    $posts = get_posts( $args );
    $authors_used = array();
    $list = "<ul>";
    
    foreach ( $posts as $post ) {
    	setup_postdata( $post );
    
    	if ( ! in_array( get_the_author_meta( 'ID' ), $authors_used ) ) {
    		$list .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    		$authors_used[] = get_the_author_meta( 'ID' );
    	}
    
    	if ( 10 == count( $authors_used ) )
    		break;
    }
    
    $list .= "</ul>";
    
    echo $list;
  6. williamchan04
    Member
    Posted 1 year ago #

    It works! This code does exactly what I want, Thankyou Jacobs
    Still don't understand what setup_postdata() does after reading codex.. anyone can explain?

  7. Timothy Jacobs
    Member
    Posted 1 year ago #

    Glad to hear it.

Topic Closed

This topic has been closed to new replies.

About this Topic