Blog Sections Open

Counting Jot Comments in Ditto with One Query

A performant way to show comment counts next to Ditto results without running Jot over every row.

Showing comment counts next to article teasers seems simple until performance becomes a problem. The naive solution is to run Jot-related logic for every listed document, but that quickly creates too many database queries.

This older pattern solved the problem with a small Ditto extender: first fetch grouped counts from the comments table, then expose the value as a placeholder like [+jotcount+] for each listed resource.

Key implementation pattern

$result = $modx->db->select('uparent, COUNT(*)', $modx->getFullTableName('jot_content'), 'published=1 AND deleted=0 GROUP BY uparent');
$counts = $modx->db->makeArray($result);
foreach ($counts as $row) {
    $jotcount[$row['uparent']] = $row['COUNT(*)'];
}

Then the template can output the precomputed count without additional per-document queries.

Why it matters historically

This is a good example of old Evo optimization culture: instead of accepting slow snippet composition, people wrote small extenders that turned repeated runtime work into one grouped query.

Why this belongs in the timeline

This post matters because it shows how Evolution CMS teams solved real project problems with small, explicit patterns instead of heavy abstractions. That practical, incremental style is one of the clearest through-lines in the old Evo ecosystem.

Newer post

Conditional Teaser Output in Ditto with TVs and PHx

A practical pattern for showing different teaser fragments depending on which TV values exist.

Older post

Keeping Ditto Pagination Placeholders Working When PHx Is Involved

A parser-order workaround for keeping Ditto pagination visible on PHx-driven pages.