You need to login to send post. No account yet? Create one:
Create account

Unpublish nodes by CRON in Drupal 6

2 replies [Last post]
admin

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.

  1. Create new content type with CCK field named 'field_admin_publishing', type 'text -> selection list', allowed values 'Yes' and 'No'.
  2. 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.

Lone Wolf
Not works for me...

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

admin
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;
}
?>