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, biapar, Willr, Ingo, swaiba, simon_w

Query for published pages only


Reply

4 Posts   120 Views

Avatar
Bagzli

17 August 2014 at 12:12pm (Last edited: 17 August 2014 12:12pm), Community Member, 37 Posts

Hello,

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 %>
<p>abc</p>
<% else %>
<p>efg</p>
<% end_if %>

What would be the keyword I use?

Avatar
Willr

17 August 2014 at 6:55pm Forum Moderator, 5511 Posts

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 site.com/?stage=Live and see if pages still show up.

If you explicitly want to get content by stage use Versioned (http://api.silverstripe.org/3.1/class-Versioned.html)

Avatar
Bagzli

18 August 2014 at 12:17pm Community Member, 37 Posts

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

Avatar
Kirk

18 August 2014 at 2:35pm (Last edited: 18 August 2014 2:41pm), Community Member, 42 Posts

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

[url]http://doc.silverstripe.com/framework/en/topics/versioning[/url]

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 %>
<p>$Title</p>
<% end_loop %>