Hook for bulk node modifications I'm trying to unpublish nodes where a field = 'Closed'. This field can change so I need to run this bulk operation automatically. I've tried these hooks:


hook_update_N 
hook_flush_caches 
hook_cron


The only hook I've had success with is a view hook (hook_view_pre_render) and sure enough my bulk operation worked. Here's my code for the operation:

$query = new EntityFieldQuery();

$query-&gt;entityCondition('entity_type', 'node')
  -&gt;propertyCondition('type', 'custom_type')
  -&gt;propertyCondition('status', 1)
  -&gt;fieldCondition('field_status', 'value', array( 'Closed' ), '=' );

$result = $query-&gt;execute();

if (!empty($result['node'])) {
  $nids = array_keys($result['node']);


 foreach ($nids as $nid) {
    $node = node_load($nid, NULL, TRUE);
    // do something awesome
    $node-&gt;status = 0;
    node_save($node);
  }
}
Where you place the code depends on when you want it to run. Usually these things goes best in hook_cron(), but cron needs to be set up to run for that to work.

Instead of doing bulk update you might want to consider changing the published value in hook_node_presave() instead, i.e. when saving the node. Example (untested):

function mymodule_node_presave() {
  $w = entity_metadata_wrapper('node', $node-&gt;original);
  $var = $w-&gt;field_status-&gt;value();
  if ($var == 'Closed') {
    $w-&gt;status = 0;
    $w-&gt;save();
  }
}


Concerning your code otherwise it should work afaik, but it would be better to load all nodes at ones. Something like this:

$nodes = node_load_multiple(array_keys($result['node']));
foreach ($nodes as $node) {
  $w = entity_metadata_wrapper('node', $node);
  $w-&gt;status = 0;
  $w-&gt;save();
}


Cheers,