Jump to content

Auto-unpublish in the back end when page date field is older than N?


MarcC
 Share

Recommended Posts

I have this task:

For any page with "event" template and a date field that is showing a date older than date N, unpublish the event.

I need this to happen fairly regularly--is this a good task for lazy cron?

Thanks!

Link to comment
Share on other sites

Just add something like this to the top of your events index page. You could compartmentalize this into a LazyCron call, but for such a simple thing it's really not necessary. 

$events = $pages->find('parent=/events/, date<"-3 months"'); 
foreach($events as $event) {
  $event->of(false);
  $event->addStatus(Page::statusUnpublished); 
  $event->save(); 
}


Btw, when testing something out like this, always comment out the $event->save(); the first time and take a look at what pages get affected by echoing them rather than saving them, the first time around. i.e. 

// $event->save();
echo "<p>Unpublishing: $event->url</p>";

Once confirmed that it is behaving the way you want, then remove the echo and uncomment your save(); 

  • Like 9
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...