Make sure grouped term exists before using it as an object.

Fixes Trying to get property of non-object in… PHP notice.