Drupal - How to programmatically list member entities of an Organic Group?

Using directly the Organic Groups API:

og_get_group_members_properties($yourGroupNode, array(), 'members', 'node');

gives you an array of user ids.

To obtain only active, pending or blocked users, you can put 'members__1', 'members__2' or 'members__3' in place of 'members', like in this example:

og_get_group_members_properties($yourGroupNode, array(), 'members__1', 'node');

of, if you prefer, like this one:

og_get_group_members_properties($yourGroupNode, array(), 'members__' . OG_STATE_ACTIVE, 'node');

Example of the returned value:

Array
(
    [0] => 48
    [1] => 49
    [2] => 51
)

Or via DBTNG:

$query = db_select("og_membership", "ogm");
$query->condition("ogm.gid", $yourGroupID, "=");
$query->condition("ogm.group_type", "node", "=");
$query->fields("ogm", array("entity_type", "etid"));
$result = $query->execute();
print_r($result->fetchAll());

...which allows you to join other tables, rather than having to filter the result set in PHP code as you would if using EntityFieldQuery.


$query = new EntityFieldQuery();
$query
  ->entityCondition("entity_type", "og_membership", "=")
  ->propertyCondition("gid", $yourGroupID, "=");
$result = $query->execute();
print_r($result["og_membership"]);

(Based on http://api.drupal.org/api/drupal/includes--common.inc/function/entity_load/7#comment-14544)