Jump to content

Module with cronjob to trash pages doesnt work


Juergen
 Share

Recommended Posts

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

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?

  • Like 1
Link to comment
Share on other sites

:-)

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 by kongondo
  • Like 1
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...