Drupal: Switching Content Types the Easy Way
One thing that I've had to do every now and then is switch a node between one content type (for instance, 'story'), and another (a new content type 'blog post').
I used to go into the database via PHPMyAdmin, and change the nodes in the node table, but that was getting quite tedious, and would take forever for a list of a hundred or thousands of nodes!
Instead, I wrote up a nice little script that you can paste directly into the 'Execute PHP' block provided by the excellent Devel module:
<?php
return;
// Paste the following into the 'Execute PHP' Devel page to update nodes to a certain content type.
// Put a comma-separated list of all the nids you'd like to change in this array
$nodes_to_switch = array(7402, 7409, 7410, 7411, 7412, 7413, 7414, 7415, 7416, 7418, 7419, 7420, 7421, 7422, 7423, 7424, 7428, 7429, 7429, 7430, 7431, 7432, 7433, 7434, 7435, 7435, 7436, 7437, 7438, 7438, 7441, 7442, 37143, 37144);
// This function will update all the nodes in the node table to the proper type
// You will need to change the type = '<typename>' to whatever you need:
foreach ($nodes_to_switch as $nids) {
db_query("UPDATE {node} SET type = 'post' WHERE {node}.nid = %d", $nids);
print "$nids - You have done one more nid!\n";
}
?>Just add a comma-separated list of node IDs you'd like to switch, and run the script. Another nice thing about this approach is that you can use Views to generate the list; just set your Views filters, then use 'Fields' as the display type, and then choose "Node: nid" for the only field to display. Copy out the list of nids, and paste them into the array.












Comments
My only concern with this would be how CCK responds to it. Supposing you were converting from a "page" to a "blog"... you would have a lot of data lost in the content_type_page table.
Ah, forgot to mention: if you have fields associated with the content type that you are switching from, you simply need to be sure that the same fields are used in the content type to which you are switching. Thus, all the data will be preserved.
Advancing the faith.
This is a very useful post, the kind of thing I've been looking for for a looong time thanks.
Awsome community, by the way.
Thanks! Glad to be helpful!
Advancing the faith.
I did something similar to this just yesterday for a content import. If you want to switch everything with one query instead of several, look into the db_placeholders function.
<?php
$nodes_to_switch = array(7402, 7409, 7410, 7411, 7412, 7413, 7414, 7415, 7416, 7418, 7419, 7420, 7421, 7422, 7423, 7424, 7428, 7429, 7429, 7430, 7431, 7432, 7433, 7434, 7435, 7435, 7436, 7437, 7438, 7438, 7441, 7442, 37143, 37144);
// This function will update all the nodes in the node table to the proper type
// You will need to change the type = '<typename>' to whatever you need:
db_query("UPDATE {node} n SET type = 'post' WHERE n.nid IN (" . db_placeholders($nodes_to_switch, 'int') . ")", $nodes_to_switch);
print implode(', ', $nodes_to_switch) . " - You have done " . count($nodes_to_switch) . " nids!\n";
For anyone looking for code-free solution, there's a module that does this. It even comes with a "fields = dead" warning.
http://drupal.org/project/nodetype
Very nice; as always, there's a module for that™
Advancing the faith.
Post new comment