$cats = get_categories();
foreach ($cats as $cat) { ?>
<div class="title">
<h2><?php echo $cat->cat_name; ?></h2>
<div class="city_box">
<span><?php echo $cat->count; ?> municipalities</span>
<?php
query_posts('posts_per_page=-1&post_type=cities&cat=' . $cat->cat_ID);
$sum_population = 0;
while (have_posts()) { the_post();
$category = get_the_category();
if ($category[0]->cat_ID == $cat->cat_ID) {
$sum_population = get_field('city-population');
$sum_population += (int)$sum_population;
}
}
echo '
<p>Population:' . $sum_population . '</p>'
?>
</div>
</div>
<?php }