Juergen Posted February 8, 2016 Share Posted February 8, 2016 Hello @ all, I have created a simple module which uses a cronjob to trash pages of certain templates after the end date. I think my code should be correct, but nothing happens. Here is my code: <?php class HookForEvents extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Cronjob to trash events', 'summary' => 'Add a cronjob to delete events', 'version' => 1, 'autoload' => true ); } public function init() { $this->addHook("LazyCron::everyMinute", $this, "trashevent"); } public function trashevent(HookEvent $e) { //put event pages to trash after the end date $eventpages = wire("pages")->find("template=single-event|events"); foreach ($eventpages as $eventpage) { $enddate = $eventpage->publish_until; $currenttime = time(); if ($enddate < $currenttime) { $pages->trash($eventpage); } } } } ?> The cronjob should only delete pages with the templates "events" or "single-event". The condition ist that the current time must be greater than the enddate (comparison). If the condition is true, then the page should be trashed. Maybe someone find a reason why this code doesnt work. Link to comment Share on other sites More sharing options...
adrian Posted February 8, 2016 Share Posted February 8, 2016 A really cursory look suggests it's because of $pages->trash - remember $pages is not available inside a function. Try wire('pages') like you have above. Link to comment Share on other sites More sharing options...
Juergen Posted February 8, 2016 Author Share Posted February 8, 2016 No luck with wire('pages') at the moment. I have triggered the page in front- and backend but the pages are still there. Maybe it has something to do that the pages are unpublished at the moment? Link to comment Share on other sites More sharing options...
adrian Posted February 8, 2016 Share Posted February 8, 2016 No luck with wire('pages') at the moment. I have triggered the page in front- and backend but the pages are still there. Maybe it has something to do that the pages are unpublished at the moment? Quite possibly - have you checked that the $eventpages array is containing the pages you are wanting to delete? What about if you add "include=all" to the selector? 1 Link to comment Share on other sites More sharing options...
kongondo Posted February 8, 2016 Share Posted February 8, 2016 (edited) Pages with hidden or unpublished status will not appear in the results from database-querying selectors that can return multiple pages (i.e. functions that return thePageArray type). For instance $pages->find("..."), $page->children(), etc. In addition, pages http://processwire.com/api/selectors/#access_control Edited February 8, 2016 by kongondo 1 Link to comment Share on other sites More sharing options...
Juergen Posted February 8, 2016 Author Share Posted February 8, 2016 What about if you add "include=all" to the selector? What a silly mistake. This was the reason - now it works!!! Thanks Adrian 1 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