Get all goups current user is member of(OG)

Public

returns all groups current user is member of
let say user is member of group id 1 and group id 2
this function will return the output in a format as 1+2 , this way we can call the views contextually to get the content belonging to groups current user is member of.

Get raw version
php
  1. function _mymodulename_get_logged_in_user_group_ids() {
  2. global $user;
  3. if ($user->uid != 0) {
  4. $group_id = '+';
  5. $current_user = user_load($user->uid);
  6. $get_group_ids = $current_user->og_user_node['und'];
  7. foreach ($get_group_ids as $get_group_id) {
  8. $group_id .= $get_group_id['target_id'] . '+';
  9. }
  10. $group_id = ltrim($group_id, '+');
  11. $group_id = rtrim($group_id, '+');
  12. return $group_id;
  13. }
  14. }