I'm trying to get all grandchildren of a specific resource in a snippet. These grandchildren also have a different template (7) than the children.
Tried all sorts of things, but can't seem to get it working.
I tried this:
$total = $modx->getChildIds(58,2,array('context' => 'web', 'template' => 7, 'published' => 1, 'deleted' => false));
But I'm afraid it doesn't work this way. If I count that array I get all the children and grandchildren. In other words, the extra conditions don't work.They are not mentioned in the rtfm but I thought I could try.
I also tried this:
$nrOfGrandchildren = 0;
$container = $modx->getObject('modResource',array(
'id' => 58
));
$containerChildren = $container->getMany('Children');
if (!$containerChildren) {
// do nothing
} else {
foreach ($containerChildren as $child) {
$childChildren = $child->getMany('Children');
foreach ($childChildren as $grandChild) {
if ($grandChild->get('template') == '7') {
$nrOfGrandchildren = $nrOfGrandchildren + 1;
}
}
}
}
But this gives me a way to high number (like 3 times the actual total of children and grandchildren).
What am I doing wrong? Is there an easy way of just getting the grandchildren of a resource?