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

    My idea:
    i need a snippet to get the child-id that can be sorted by menuindex. (It must be possible to get any child-id seperately $number)

    i try to work with this snippet but cant sort by menuindex:

    <?php
    /* build a query to get Resources with a given parent that are published and not deleted */
    $query = $modx->newQuery('modResource', array('parent' => $id, 'deleted' => false, 'published' => true));
    
    /* get the modResource objects with the given parent */
    $children = $modx->getCollection('modResource', $query);
    
    /* loop through the children and push the id onto $resourceIds */
    $resourceIds = [];
    $i=0;
        foreach ($children as $child) {
        $resourceIds []= $child->get('id');
    }
    return $resourceIds[$number];


    Do you have any ideas or suggestions?

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
      you will need to add sortby.
      If you want to get only one object you can also use limit and offset.
      Would be much faster with many resources.

      <?php
      /* build a query to get Resources with a given parent that are published and not deleted */
      $query = $modx->newQuery('modResource', array('parent' => $id, 'deleted' => false, 'published' => true));
      $query->sortby('menuindex');
      $query->limit(1,$number);
       
      /* get the modResource objects with the given parent */
      $resourceID = 0;
      if ($child = $modx->getObject('modResource', $query)){
          $resourceID = $child->get('id');
      }
       
      return $resourceID;
      







        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 51347
        • 82 Posts
        Perfect! You make my day Bruno, big thanks!