Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » Getting Random Children

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 625 Views
  • nitromike
    Community Member
    3 Posts

    Getting Random Children Link to this post

    I'm trying to retrieve random children of a product group page. I can retrieve the product group page from teh template, but I can't call any more custom functions. currently in my Page.php I'm using

    function RandomChildren($count = 2)
    $pages = DataObject::get("SiteTree","ParentID = $this->ID","RAND()",null,$count);
    return $pages;


    And to call in the template I'm using

    <% control RandomChildren(2) %>
    <% end_control %>

    But the Debug isn't even showing, and I'm in "Dev" mode

    I've tried other methods, and also found that at one point, if I retrieve a page using

    return DataObject::get('SiteTree','ParentID = $this->ID);

    I will debug and see the results of the object as expected,

    but when I use this to get random children it fails, if i debug the ID of the object, it's empty

    $page = DataObject::get('SiteTree','ParentID = $this->ID)

    This site is very complicated, but the logistics should work. I've used this code before on other projects, and have never seen this problem, any thoughts anyone???

  • gaethofs
    Community Member
    9 Posts

    Re: Getting Random Children Link to this post

    What version of SS are your using?

    You could do something like this:

    function RandomChildren($count = 2) {
       return SiteTree::get()->filter('ParentID', $this->ID)->sort('RAND()')->limit($count);

    I gave this a quick try and it works fine. Hope this works for you!

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.