titanium Posted October 4, 2013 Share Posted October 4, 2013 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 More sharing options...
adrian Posted October 4, 2013 Share Posted October 4, 2013 Make sure you have debug mode on so you can see any errors that are thrown. It might be as simple as needing this before the save line: wire('page')->of(false); http://wiki.processwire.com/index.php/Text_Formatters http://cheatsheet.processwire.com/?filter=of(true Link to comment Share on other sites More sharing options...
titanium Posted October 5, 2013 Author Share Posted October 5, 2013 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 More sharing options...
Wanze Posted October 5, 2013 Share Posted October 5, 2013 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); 3 Link to comment Share on other sites More sharing options...
titanium Posted October 5, 2013 Author Share Posted October 5, 2013 D'oh! Embarrassing for me Link to comment Share on other sites More sharing options...
blackeye1987 Posted September 30, 2014 Share Posted September 30, 2014 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 More sharing options...
Soma Posted September 30, 2014 Share Posted September 30, 2014 Save hook save hook save hook ... Add a flag to the page on first save and check for it to terminate. Link to comment Share on other sites More sharing options...
blackeye1987 Posted September 30, 2014 Share Posted September 30, 2014 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 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 Link to comment Share on other sites More sharing options...
Soma Posted September 30, 2014 Share Posted September 30, 2014 You shouldn't exit (actually never) but use a flag ... if($page->skip) return; $page->skip = true; $page->save(); ... Link to comment Share on other sites More sharing options...
blackeye1987 Posted September 30, 2014 Share Posted September 30, 2014 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 Link to comment Share on other sites More sharing options...
Soma Posted September 30, 2014 Share Posted September 30, 2014 Ah ok. On a sidenote: It's recommended better to use add and remove status not just status = status cause it's a bitmask and it can have more than one status. $page->addStatus(Page::statusUnpublished); https://processwire.com/talk/topic/1293-how-to-set-unpublished-for-a-page-in-the-api/?p=11538 2 Link to comment Share on other sites More sharing options...
blackeye1987 Posted September 30, 2014 Share Posted September 30, 2014 i know saw that, in diffrent posts. but due to restrictions of users they are not able to set these. the unpublished state is used to indicate that they dont need to fill the topic. its complicated to explain. but thanks for the reminder 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