Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Sort and Filter children


Go to End


5 Posts   2371 Views

Avatar
BuckeyeSam

Community Member, 7 Posts

8 June 2015 at 10:56am

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 of 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?

Avatar
Pyromanik

Community Member, 419 Posts

9 June 2015 at 9:08am

$this->AllChildren()

Children does not show anything that either:
a) a user doesn't have permission for (same for allchildren)
b) has 'ShowInMenus' set to false.

Avatar
BuckeyeSam

Community Member, 7 Posts

9 June 2015 at 9:42am

Edited: 09/06/2015 10:28am

$this->AllChildren()

That doesn't work at all. No articles show up and in fact, it breaks the page. I get a white screen. There is only one user for the back end. That is the default admin account. There are no users for front end. Everything is set to show in menu. Like I said in the original post this code works:

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

It's when I try to add a filter that it breaks. Thanks for trying though :(

Avatar
Pyromanik

Community Member, 419 Posts

9 June 2015 at 11:07am

Oh, sorry I didn't read properly.

So you're either looking to add to the filter when using ArticlePage::get() - 'ParentID' => $this->ID

Or using the string 'now' instead of SS_Datetime::now() - although that may return a string.
And it doesn't explain why AllChildren gives you an error (put your site into dev mode to see it).

It could be an issue with ArrayList and filter modifiers. Not sure though.

Avatar
BuckeyeSam

Community Member, 7 Posts

9 June 2015 at 11:32am

With your help I was able to come up with this that worked. Thanks!

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