How to sort and filter children in Silverstripe

818 views Asked by At

I have this function:

public function LatestArticles() {
    return $this->Children()
        ->filter('PublicationDate:LessThanOrEqual', SS_Datetime::now())
        ->sort('PublicationDate','desc');
}

It's not outputting anything at all. If I switch it to this:

public function LatestArticles() {
    return ArticlePage::get()
        ->filter('PublicationDate:LessThanOrEqual', SS_Datetime::now())
        ->sort('PublicationDate','desc');
}

It outputs my ArticlePages, but that (obviously) includes all ArticlePages site wide, not just the Children ArticlePages.

If I change the code to this:

public function LatestArticles() {
    return $this->Children()
        ->sort('PublicationDate','desc');
}

That outputs all my children ArticlePages but that includes articles that have a publication date later than today.

It seems to me that I'm maybe not using the filter function correctly. Where am I going wrong?

1

There are 1 answers

0
AudioBubble On BEST ANSWER

I cross posted this question on another site and got an answer that worked. Below is the edit to the code that was needed.

public function LatestArticles() {
    return ArticlePage::get()
        ->filter(array(
            'ParentID' => $this->ID,
            'PublicationDate:LessThanOrEqual' => SS_Datetime::now()
        ))
        ->sort('PublicationDate','desc');
}