$q = $modx->newQuery('modResource', $id);
$q->leftJoin('TicketThread','TicketThread', "`TicketThread`.`name` = 'resource-{$id}'");
$q->leftJoin('TicketComment','TicketComment', "`TicketThread`.`id` = `TicketComment`.`thread`");
$q->where(array(
    'TicketComment.published' => 1
));

$q->select('COUNT(`TicketComment`.`id`) as `comments`');

$count = 0;
if ($q->prepare() && $q->stmt->execute()) {
	$count = (integer) $q->stmt->fetch(PDO::FETCH_COLUMN);
}
return $count;