gebeer Posted December 9, 2013 Share Posted December 9, 2013 Hello, I need to find a good way to check for the status of a page, whether it is unpublished or not. If I do echo $page->status I get these values: published: 1 unpublished: 2049 hidden and published: 1025 hidden and unpublished: 3073 My page is hidden by default. So to check for unpublished state I do if ($page->status == 3073) which is working fine. Only thing that worries me is that I know nothing about these status codes and whether it will always be 3073 for hidden and unpublished. So if you know a more generic way of doing this, please share it here. Thank you. Cheers gerhard Link to comment Share on other sites More sharing options...
teppo Posted December 9, 2013 Share Posted December 9, 2013 Try $page->is() with status code flags. Example: if ($page->is(Page::statusUnpublished)) if (!$page->is(Page::statusUnpublished)) 6 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted December 9, 2013 Share Posted December 9, 2013 (edited) Or check with a string: `hidden, locked, unpublished, system, systemID` $page->is('unpublished'); (Teppo was quicker !) Edited December 9, 2013 by Martijn Geerts 5 Link to comment Share on other sites More sharing options...
gebeer Posted December 9, 2013 Author Share Posted December 9, 2013 @teppo & Martijn thanks a lot. I knew there would be a better way to do this EDIT Martijns solution doesn't seem to work in my case. I first get the page by ID $aktuell = $pages->get(1041); If I then do if (!$aktuell->is('unpublished')) the if condition is being ignored. But with if (!$aktuell->is(Page::statusUnpublished)) everything behaves as expected. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted December 9, 2013 Share Posted December 9, 2013 maybe i'm wrong, but so far I see this should function. /** * Does this page have the specified status number or template name? * * See status flag constants at top of Page class. * You may also use status names: hidden, locked, unpublished, system, systemID * * @param int|string|Selectors $status Status number, status name, or Template name or selector string/object * @return bool * */ public function is($status) { if(is_string($status) && isset(self::$statuses[$status])) $status = self::$statuses[$status]; return $this->comparison()->is($this, $status); } 1 Link to comment Share on other sites More sharing options...
teppo Posted December 9, 2013 Share Posted December 9, 2013 (edited) @gebeer: Have you tried if "Unpublished" (capital "U") works? Just a guess, but it could be that Edit: my guess was wrong; lowercase would work just fine. The issue is that the code Martijn posted above is from dev branch. If you're running stable (master) branch of ProcessWire, that won't work for you. Edited December 9, 2013 by teppo 4 Link to comment Share on other sites More sharing options...
gebeer Posted December 10, 2013 Author Share Posted December 10, 2013 Thank you for clarifying this. I'm on stable branch. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now