We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38813
    • 29 Posts
    Hi everyone,

    I've been playing with MODX over the past week or so and I've been digging into the api and playing with some of the functionality.

    One thing I haven't been able to find is an obvious way to get all children of the root of the context.

    For example, from the demo I've installed, the root of the context is 'web'... is there a convenient way for me to find all children of it?

    I'm not interested in grandchildren (that's for next week), just the immediate children.

    Thanks a million,

    M
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      $children = $modx->getCollection('modResource', array( 'context_key' => 'web', 'parent' => 0));
      

      which is the same as
      $c = $modx->newQuery('modResource');
      $c->where(array(
        'context_key' => 'web',
        'parent' => 0,
      ));
      $children = $modx->getCollection('modResource',$c);
      

      and you can use that array of modResource objects like this:
      foreach ($children as $child) {
        var_dump($child->toArray());
      }
      


      To get the children of the current resource you can do this:
      $children = $modx->resource->getMany('Children');

      or
      $c = $modx->newQuery('modResource');
      $c->where(array(
        'context_key' => 'web',
        'parent' => $modx->resource->get('id'),
      ));
      $children = $modx->getCollection('modResource',$c);
      
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 38813
        • 29 Posts
        Thanks so much Mark. I'm coming from an Etomite background and I'm still in shock from the extent of the MODX API. Think I'm trying too hard...

        Your answer is very much appreciated.

        Michael
          • 18373 ☆ A M B ☆
          • 3,141 Posts
          The Revolution API is *very* different from Evo / Etomite... that's for sure wink
            Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

            Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.