You need to login to send post. No account yet? Create one:
Create account
Unpublish nodes by CRON in Drupal 6
Sun, 02/07/2010 - 14:22
Webdesign is not only design, but a behavior of site itsels. (Maybe it is main reason why Drupal is exists :-) Sometimes there is unusual demand - keep the new content live a while only. Then let it unpublished and webmaster will decide if like it or not... There is solution.
- Create new content type with CCK field named 'field_admin_publishing', type 'text -> selection list', allowed values 'Yes' and 'No'.
- Create module with this PHP:
<?php
/**
* Implementation of hook_cron
**/
function MY_MODULE_cron() {
$result = db_query("SELECT nid FROM {node} WHERE type = 'MY_CONTENT_TYPE'");
while ($event = db_fetch_object($result)) {
$node = node_load($event->nid);
if (($node->status == 1) && ($node->field_admin_publishing[0]['value'] != 'Yes')) {
db_query("UPDATE {node} SET status = 0 WHERE nid = %d", $event->nid);
watchdog('event', t('Removed for %event by cron.', array('%event' => $node->title)));
}
}
}
?>
Done! Good work. Every CRON cycle (cron.php is call) will checked all content of your new content type, and every one, which will not have set 'Yes' in 'field_admin_publishing' will set as unpublished.
Sun, 02/14/2010 - 16:18
#2
Sorry I forgot...
In my case I unpublishing content created by anonymous users. To work with content by authenticated user you have to authenticate cron too. In example by this snippet:
<?php
function MY_MODULE_cron() {
global $user;
$current_user = $user;
$user = user_load(array('name' => 'USER_WHO_CAN_(UN)PUBLISH_IT'));
$result = db ...
... $node->title)));
}
}
$user = $current_user;
}
?>



















This snippet working only when I'm logged AND manually use cron.php...