Jump to content

Problem with setting a page to be unpublished by API


titanium
 Share

Recommended Posts

Dear ProcessWire fans,

I'm trying to set a page to be unpublished by API. Reason is: the page contains a field with a timestamp. As soon as the timestamp is reached, the page should become automatically unpublished.

As far as I know, the following simple code in the page's template should do:

if($timestamp <= time()) {
	wire('page')->addStatus(Page::statusUnpublished);
	wire('page')->save;
	throw new Wire404Exception();
}

The 404 is thrown, but the status doesn't get updated. If I edit the page in the admin afterwards, it is still published...

Link to comment
Share on other sites

Thanks, Adrian. My test case is like this (I put it in a template):

var_dump(wire('page')->status);

wire('page')->addStatus(Page::statusUnpublished);
wire('page')->of(false);
wire('page')->save;
wire('page')->of(true);

var_dump(wire('page')->status);
exit;

When the page is called in the browser the output is:

int(1)

int(2049)

As far as I know, the page should have the status 2049 now, as int(2049) indicates. It should have been saved. But if I reload the page, I do get the same result! I expected int(2049), int(2049). What could be wrong with this?

Link to comment
Share on other sites

You must set the output formatting to false before changing the status.

Also you need to write save(), because it's a method and not a property:

wire('page')->of(false);
wire('page')->addStatus(Page::statusUnpublished);
wire('page')->save();
wire('page')->of(true);
  • Like 3
Link to comment
Share on other sites

  • 11 months later...

hi got a similar problem

i have a hook on aftersave which does in the end

if(!$page->is(Page::statusUnpublished)){
 $page->of(false);
 $page->status = Page::statusUnpublished;
 $page->save();
}

if i put out 

id: status(before change) status (after change)

i get this:

1310:int(0) int(2048) 

in basically an endless loop.

so it doesn't save the state, but why ?

Link to comment
Share on other sites

well this would be a workaround but it wouldn't help since the page doesn't get saved...
the status stays published.
so even if i exit the function on second call the page stays without beeing edited.



ahhhhh nevermind.. i found the error.. and it wasn't in the code :D

i set the template settings to do not allow to unpublish pages,
since i forgot what i was doing yesterday and testing today's frontent.. sooo.. jeah.. my bad.. nvm guys :D

Link to comment
Share on other sites

i didn't ment to use "<?php exit; ?>" to extit the function.

i already use return to get out it was depending on the published state of the page.

since i could not set it anymore it was called again.

it looks like this in my func

if(!$page->is(Page::statusUnpublished)){
 $page->of(false);
 $page->status = Page::statusUnpublished;
 $page->save();
}
return;

i just got into trouble because of the user ability to think straight :D

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...