TemplateReport snippet: List the templates used by the site and the number of published and unpublished pages each template uses
<?php
/*
TemplateReport snippet
list the templates used by the site
and the number of published and unpublished pages each template uses
Author: http://forums.modx.com/u/BobRay
Source: http://forums.modx.com/thread/94635/simple-count-of-pages-using-a-particular-template#dis-post-512567
*/
/* [[TemplateReport]] */
$templates = $modx->getCollection('modTemplate');
$output = '<h3>Template Report</h3>';
$output .= "\n<ul>";
$published = array('published' => '1');
$unpublished = array('published' => '0');
foreach ($templates as $template) {
$id = $template->get('id');
$name = $template->get('templatename');
$c1 = $modx->newQuery('modResource');
$c1->where(array(
'published' => '1',
'template' => $id,
));
$numPublished = $modx->getCount('modResource', $c1);
$c2 = $modx->newQuery('modResource');
$c2->where(array(
'published' => '0',
'template' => $id,
));
$numUnpublished = $modx->getCount('modResource', $c2);
$output .= "\n" . '<li><b>' . $name . '</b>';
$output .= "\n<ul>";
$output .= "\n" . ' <li>Published resources: ' . $numPublished . '</li>';
$output .= "\n" . ' <li>Unpublished resources: ' . $numUnpublished . '</li>';
$output .= "\n</ul>";
$output .= "\n</li>";
}
$output .= "\n</ul>";
return $output;