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

    Hello

    This is a very basic snippet question for revo.

    I am trying to fetch the children of a certain fixed ressource, with this script:

    $docArray = $modx->getChildIds(5,1,array('context' => 'web')); // 5 is the ID of the ressource, which children I need to list.

    The problem is, that this also returns unpublished children.

    Is there an easy way to filter out unpublished ressources?

    I have looked at the documentation, but I can only find something like this:

    $docArray = $modx->getCollection('modResource',array(
    'published' => 1,
    'searchable' => 1
    ));

    How do I get this to work with the scenario described above?

    All the best

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

      • 46886
      • 1,154 Posts
      I think its something like this:

         $docArray->where(array(
          'published' => '0',
          ));
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Can't you just add 'published'=>'1' to the options array?
        $docArray = $modx->getChildIds(5,1,array('published' => '1'));
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 46886
          • 1,154 Posts
          Ooops I was way wrong. lecable, pls listen to Susan and not me. She is the true expert.
          • discuss.answer
            • 3749
            • 24,544 Posts
            If you are actually going to use the child objects for something, you can also do this to get them in a single query:


            $parentDoc = $modx->getObject('modResource', 5);
            
            if ($parentDoc) {
                $c = $modx->newQuery('modResource');
                $c->where(array(
                    'published' => 1,
                    'searchable' => 1,
                    'deleted' => 0,
                ));
                $children = $parentDoc->getMany('Children', $c);
            
                foreach ($children as $child) {
                    $output .= $child->get('pagetitle');
                }
            } else {
                $output = 'Parent not found';
            }
            return $output;
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 49680
              • 19 Posts
              Thanks for your replies. I will take a look at the suggestions.
              I think I have to look more at the -> and => notations, as I am not so familiar with these...


                • 49680
                • 19 Posts
                hmm. This is strange.

                When I try sottwells solution
                $docArray = $modx->getChildIds(5,1,array('published' => '1'));
                I still get the unpublished resources as well

                I have also tried Bobrays code directly in a snippet which no addings and I get a blank page and a php-error:
                [27-Mar-2015 08:41:19 Europe/Copenhagen] PHP Parse error: syntax error, unexpected 'if' (T_IF) in /Applications/MAMP/htdocs/modX-revo/core/cache/includes/elements/modsnippet/1.include.cache.php on line 6

                As I think these codes are correct, something else must be wrong?

                All the best
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  The options array in my suggestion is probably only applying to the parent resource, not the children.

                  The "if" error sounds like a missing ; at the end of a previous line of code.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 49680
                    • 19 Posts
                    Quote from: sottwell at Mar 27, 2015, 04:29 AM
                    The options array in my suggestion is probably only applying to the parent resource, not the children.

                    Ok, so this won't work...

                    Quote from: sottwell at Mar 27, 2015, 04:29 AM
                    The "if" error sounds like a missing ; at the end of a previous line of code.

                    No. I deleted everything else. It is Bobray's code alone, that causes the error. But I can't figure out what is wrong.
                      • 46886
                      • 1,154 Posts
                      Heh my solution is from a friend of mine who is a pretty good coder. You might give it a try. I try to stay within my area of some knowledge, but heck why not give it a shot?