Show only visible items on homepage from section

Silverstripe Version:

I have on HomePage 3 lastest articles (type: Development) from Sections but I want show only that those are “ShowInSection” (which is checkbox in admin)

Details of your query go here

class HomePage_Controller extends PageController {
		public function LatestArticles($count = 3)
    {
        return Development::get()
			->sort('Created', 'DESC')
                        ->limit($count);
    }
}

You can use filters to narrow down the results:

return Development::get()
  ->filter(['ShowInSection' => true])
  ->sort('Created', 'DESC')
  ->limit($count);

There are different kinds of filters and exclude methods… have a look here for more details: https://docs.silverstripe.org/en/4/developer_guides/model/data_model_and_orm/#filtering-results

1 Like