I have some unpublished nodes, for which I have their node IDs. How can I programmatically publish them? Is it correct to achieve it in custom module using a query like the following?
db_query("UPDATE {node} SET status = 1 WHERE nid = %d", $nid)
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityUsing db_query()
is a Drupal 6 method, which is not used in Drupal 7.
To achieve your job I suggest this code (without any manual query).
// load the node object
$node = node_load($nid);
// set status property to 1
$node->status = 1;
// re-save the node
node_save($node);
If you have more node ID, and you want to publish all those nodes, you can use the following code.
$nids = array();
// Load all nodes in one go for better performance.
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
// set status property to 1
$node->status = 1;
// re-save the node
node_save($node);
}
db_query
is a Drupal 7 method too. Just takes different arguments.
– user11312
Dec 5 '13 at 13:05
submit
handler to your from then in custom submit handler yu can use something like this.
– Yuseferi
May 23 '15 at 4:36
when you are using node_load(), node_load_multiple() for loading a(multiple) noad(s) there are many hooks would be invoked by modules, such as hook_node_load, hook_node_operations, hook_node_prepare, hook_node_accesse. But by performing query directly on drupal schema those hooks will be ignored and leads to many issues.
The Queries are:
db_query("UPDATE {node} SET `status` = '1' WHERE `nid` =:nid ;"
,array(':nid'=>$node->nid));
db_query("UPDATE {node_revision} SET `status` = '1' WHERE `nid` =:nid AND `vid` =:vid;"
,array(':nid'=>$node->nid,'vid'=> $node->vid));
Just use above code If node_load() or node_load_multiple() not working because of running out of available memory.
node_revision
, so it will cause inconsistency. – artfulrobot Sep 15 '14 at 12:20