How can I get the last posts by user role?

I would try the following (not tested)

<?php
    $friends = get_users( array( 'role' => 'friends' ) );
    $friend_ids = array();

    foreach( $friends as $friend ) 
        $friend_ids[] = $friend->ID;

    $news = new WP_Query( array( 'author' => implode( ',', $friend_ids ), 'post_type' => 'news', 'paged' => get_query_var('paged') ) );
?>

Note: ‘friends’ is the role ID, not the nice name. Then use the loop as normal :

<?php if ( $news->have_posts() ) : while ( $news->have_posts() ) : $news->the_post(); ?>  
    ...
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>

Hopefully that will work 🙂

Leave a Comment