We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6208
    • 44 Posts
    Big apologies if this has been asked and answered many times before:

    Is there a way to have the results returned by $modx->getDocumentChildren() sorted by pub_date or createdon? Or do I have to take the resulting array and do a sort on one of the keys?

    Is there a comparable function to getDocumentChildren() that does have a sorting flag built in?

    Thanks in advance. MODx rocks!

    Andrew
      • 10487 MODX Staff
      • 1,535 Posts
      Hi cyberk,

      getDocumentChildren() has a sort option built in. smiley

      If you set the sixth argument of your function call to the column you want to sort by then that should work.

      Hope that helps, Garry
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 6208
        • 44 Posts
        Thanks so much, Garry!

        After I read your response I got smart and searched for the function in document.parser.class.inc.php so I could see how the function was actually defined. Works great!

        Thanks again!

        Andrew
          • 29635
          • 361 Posts
          Hey Andrew, could you post an example of how you’re calling getDocumentChildren to sorty by pub_date? I’ve been playing with it for far too long with no success.

          Thanks!
          -Brett
            Need MODx Ecommerce? Try FoxyCart!
            • 6208
            • 44 Posts
            Hi Brett,

            Here’s the line of code I used:

            $kids=$modx->getDocumentChildren($folder,1,0,"*","","pub_date","desc");


            Assume the $folder variable is the folder you want to get the child documents of. This line of code would return the $kids array, sorted by date, and you could loop through the array any way you wished.

            Does this help? Post or email if you have further questions.

            Cheers,

            Andrew