Support » Fixing WordPress » A function to call 10 latest post with no repeated author

  • 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);
Viewing 6 replies - 1 through 6 (of 6 total)
  • Timothy Jacobs

    (@timothyblynjacobs)

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

    Thread Starter williamchan04

    (@williamchan04)

    thankyou for pointing it out

    Timothy Jacobs

    (@timothyblynjacobs)

    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?

    Timothy Jacobs

    (@timothyblynjacobs)

    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;
    Thread Starter williamchan04

    (@williamchan04)

    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?

    Timothy Jacobs

    (@timothyblynjacobs)

    Glad to hear it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘A function to call 10 latest post with no repeated author’ is closed to new replies.