Joomla: How to get a user’s K2 group

Joomla’s been driving me crazy lately, I’m sticking this code here for future reference so I can find it again, and in case someone else needs it for whatever reason. Use this to grab the K2 user group that a user is in with a Joomla install (works for 1.5, not sure about older/newer versions).

Enjoy!

$user =& JFactory::getUser();
//name of K2 user table
$k2usertable = 'nua_k2_users';
$userId = $user->get('id');
// get user Group
$db =& JFactory::getDBO();
$query = "
  SELECT ".$db->nameQuote('group')."
    FROM ".$db->nameQuote($k2usertable)."
    WHERE ".$db->nameQuote('userID')." = ".$db->quote($userId).";
  ";
$db->setQuery($query);
$userGroup = $db->loadResult();

//the user group's id
echo $userGroup;

Leave a Reply