I’m using the code below from another answer to query posts from 1 custom role, eg “friends”. I thought I could just comma separate the roles to list posts from more than one role, but this didn’t work. Is there another way I can write it? I’ve tried 'role' => 'friends', 'enemies' and 'role' => 'friends, enemies' but I guess get_users only supports one role.

<?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') ) ); ?>

2 Answers
2

Just do the get_users() twice and merge the results:

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

$friends_n_foe = array_merge($friends, $enemies);

$IDs = array();

foreach( $friends_n_foe as $person ) {
    $IDs[] = $person->ID;
}

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

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *