Limit loop to 5 posts

I am using a loop that shows one post per author. The problem is that if I have 20 authors then the loop shows 20 post. How could I limit this to display 5 posts in TOTAL?

This is my current code:

$authors=get_users(array('exclude' => get_current_user_id()));
$i=0;
//get all users list
foreach($authors as $author){
$authorList[$i]['id']=$author->data->ID;
$authorList[$i]['name']=$author->data->display_name;
$i++;
}

shuffle($authorList );
foreach($authorList as $author){
$args=array(

'showposts'=>1,
'author'=>$author['id'],
'caller_get_posts'=>1,
'post_type' => 'athlete_profile',

);

$query = new WP_Query($args);
if($query->have_posts() ) {
while ($query->have_posts()){
$query->the_post();

  • Nahid
    • Tech Support

    Hey there Nicklas !
    Hope you are having a great day!

    Could you please try using the following code for the loop instead?

    $authors=get_users(
    	array(
    		'exclude' => get_current_user_id()
    	)
    );
    
    // Get all users list
    
    $i = 0;
    foreach( $authors as $author ) {
    	$authorList[$i]['id']=$author->data->ID;
    	$authorList[$i]['name']=$author->data->display_name;
    	$i++;
    }
    shuffle( $authorList );
    
    $i = 0;
    foreach( $authorList as $author ) {
    
    	$i++;
    
    	$args=array(
    		'showposts'=>1,
    		'author'=>$author['id'],
    		'caller_get_posts'=>1,
    		'post_type' => 'athlete_profile',
    	);
    
    	$query = new WP_Query($args);
    	if($query->have_posts() ) {
    		while ($query->have_posts()){
    			$query->the_post();
    
                             // Your loop content here
    		}
    	}
    
    	if( $i == 5 ) break;
    
    }

    This should limit the total loop to five results.

    Hope this helps. Please let us know if this works for you. Thanks!

    Kind regards,
    Nahid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.