Drupal 7: change the content type of a node

If we have to change the content type of some node in Drupal 7 the simplest solution is to work directly on the database. Drupal 7 stores the content type of a node in the database in the table node under the value type and if the node has comments there are references to them in the database in the tables field_data_comment_body and field_revision_comment_body under the value bundle.

Understood how Drupal stores these informations is to simple edit the content type of a node with a qwery:

UPDATE node
SET type = 'NEW_NODE_TYPE'
WHERE nid=NODE_ID

and if we change the content type of multiple nodes that have some comment we have to update also the tables field_data_comment_body:

UPDATE field_data_comment_body
SET bundle = 'comment_node_NEW_NODE_TYPE'
WHERE entity_id IN (SELECT cid FROM comment WHERE nid IN  (SELECT nid FROM node WHERE type = 'NEW_NODE_TYPE' ))

and field_revision_comment_body:

UPDATE field_revision_comment_body
SET bundle = 'comment_node_NEW_NODE_TYPE'
WHERE entity_id IN (SELECT cid FROM comment WHERE nid IN  (SELECT nid FROM node WHERE type = 'NEW_NODE_TYPE' ))

 

Tags: