Merging two types of pages in SilverStripe

$page1 = PageType1::get();
$page2 = PageType2::get();

      

Is there a way to get the last 5 posts from a combination of two page types. Any help is appreciated.

+3


source to share


1 answer


Assuming PageType1

u PageType2

are children of the class Page

you can do:

$myPages = Page::get()->filter(['ClassName' => ['PageType1', 'PageType2']]);

      

or any other parent class of both types of pages.



You can sort by the date created (which is saved in the SiteTree table) and limit eg.

$sortedAndLimited = $myPages->sort('Created')->limit(5);

      

Downside: You cannot easily search, filter or sort for individual fields that are not used by the parent class Page

, you have to do the joins manually for that.

+4


source







All Articles