We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    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?
      • 19328
      • 433 Posts
      Alright, I was not thinking very clear.

      This seems to do what I want, using the template as criterium:

      $criteria = $modx->newQuery('modResource');
      		$criteria->where(array(
         			'template' => 7,
         			'published' => 1,
         			'deleted' => 0
      		));
      		$total = $modx->getCount('modResource', $criteria);


      I would still be interested to know if there's an easy way to get all grandchildren of a resource.