Jump to content

Best way to check for page status


gebeer
 Share

Recommended Posts

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

@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

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);
}
  • Like 1
Link to comment
Share on other sites

@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 by teppo
  • Like 4
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...