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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Query for published pages only

Go to End

4 Posts   1777 Views


Community Member, 71 Posts

17 August 2014 at 12:12pm

Edited: 17/08/2014 12:12pm


I am trying to create a query that will retrieve results only for pages that have been published. Right now when I run the query below, I will get EVERY page, whether I have hit publish on them or not. This is a problem :)

$filter = $this::get()->filter('Filters:PartialMatch', $this->request->getVar('tag'))->First();

Also, How would I check in .ss page whether the page has been published? For example I want to write code such as this:

<% if published %>
<% else %>
<% end_if %>

What would be the keyword I use?


Forum Moderator, 5523 Posts

17 August 2014 at 6:55pm

SilverStripe should handle this for you built in. You don't need to explicitly fetch stage / live. Make sure you're not viewing the site in 'stage' module (try your and see if pages still show up.

If you explicitly want to get content by stage use Versioned (


Community Member, 71 Posts

18 August 2014 at 12:17pm

Where do I set the site between different stages and what are all the stages available?


Community Member, 67 Posts

18 August 2014 at 2:35pm

Edited: 18/08/2014 2:41pm

To run a query to get pages via the stage you would need some code like below

// Fetching multiple records
$stageRecords = Versioned::get_by_stage('SiteTree', 'Stage');
$liveRecords = Versioned::get_by_stage('SiteTree', 'Live');

The url below has more info on Versioning

To get pages that have been published via a template try something like this

function getPublishedPages() {
return Versioned::get_by_stage('SiteTree', 'Live');

In the template use something like this

<% loop PublishedPages %> 
<% end_loop %>