1

I have multiple authors in my site and want to show just one, the latest post from each one, excluding the administrators. So, if for example:

  • admin1 has posted 4 posts
  • admin2 posted 3 posts
  • author1 posted 2 and
  • author2 posted 7,

it will show 4 from admin1, 3 from admin2 and 1 from each one of the others. Any hints?

<?php 
get_header(); 
//Displaying latest post per author on front page 
if(have_posts()) : 

    get_template_part('content', 'postlist'); 

else : 
    get_template_part('content', 'none'); 
endif;

get_footer();
?>

1 Answer 1

1

If I understand your problem then this should definitely work.

<?php 
get_header();

$users = get_users( array( 'who' => 'author' ) );//get all the users with author role in an array


foreach ( $users as $user ) {   //travers the array

    if($user->caps['administrator']==1)continue;     // skip the user if user also have administrative capabilities
$query = new WP_Query( array(
                            'posts_per_page'=>1, 
                            'author' => $user->ID
                            ) 
                    );
if($query->have_posts()):while($query->have_posts()):$query->the_post();

get_template_part('content', 'postlist'); 

endwhile;

else:
get_template_part('content', 'none'); 
endif;

} 

get_footer();
?>
4
  • Maybe I'm missing something but it doesn't seem to work. in my index.php i have the following: <?php get_header(); //Displaying latest post per author on front page if(have_posts()) : get_template_part('content', 'postlist'); else : get_template_part('content', 'none'); endif; get_footer(); ?>
    – maus
    Commented Jan 25, 2016 at 18:08
  • @maus didn't u try my code??
    – terminator
    Commented Jan 28, 2016 at 14:57
  • @maus i have edited my answer , give it a try , this should definitely work
    – terminator
    Commented Jan 28, 2016 at 15:32
  • First, I think the bracket } is not correct and it should be placed before if($query->have_posts()) as it displays the whole output multiple times (equal to number of authors). Second, if I remove the if($user->caps['administrator']==1)continue; it is supposed to display just one post per author (regardless if it is a admin or not), right? Well, I see no change on the final output. Everything is displayed.
    – maus
    Commented Jan 31, 2016 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.