Skip to content

Instantly share code, notes, and snippets.

@nathan-roberts
Created November 8, 2022 02:09
Show Gist options
  • Save nathan-roberts/9bb5d969ce9c3dca757602ab2b62dad0 to your computer and use it in GitHub Desktop.
Save nathan-roberts/9bb5d969ce9c3dca757602ab2b62dad0 to your computer and use it in GitHub Desktop.
Query posts by users roles
<?php
/**
* Get all the user ID's for the specified user roles
*
* @param array $role
* @return array User ID's
*/
function get_all_user_ids_by_user_roles($role)
{
$users = get_users(array('role__in' => $role));
$user_ids = array();
foreach ($users as $user) {
$user_ids[] = $user->ID;
}
return $user_ids;
}
$roles = ['editor', 'administrator'];
$user_ids = get_all_user_ids_by_user_roles($roles);
$args = array(
'author__in' => $subscriber_ids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment