Im on d7. I have a lot of nodes on my site that are all categorized with one of eight terms of my "category" vocabulary. I have been doing a theme redesign that calls for me to make a new taxonomy vocabulary that these node will be categorized in called "sections". I need a way to make all current nodes with "categories/term-1" to also have "sections/term-1", "categories/term-2" to have "sections/term-2", and so on. I was thinking I could do this with rules, but that would be a per node basis and I need this to update ALL current nodes. Any suggestions? Thanks.
1 Answer
You can use hook_update_N() to update multiple nodes at once with a single run of update.php. Inside it you can load all the nodes that you need to modify using node_load(), and after you are done changing the taxonomies programatically, call node_save().
-
Thanks, can you give me an example of how to do this? How would I use node_load() to load all nodes tagged with a term? Thanks.– oobie11Feb 21, 2012 at 5:32
-
First you would need to come up with a database query that will return all the nodes that you want to alter. One trick I often employ is to use Views to come up with the query for you, and then copy/paste/modify it to your needs. For documentation on database queries you can consult these links: db_query(), Database abstraction layer, db queries. But you can also google further information. Feb 22, 2012 at 16:45
-
1The purpose of that database query will be returning just the nids of the nodes that you want to alter. You can do a loop with the database results, and within the loop call
$node_obj = node_load($nid);
. Once you load the node you might want to try using field_get_items() to get and alter the taxonomy field, but you could end up altering the taxonomy in another way. Once you modify the taxonomy field, then call node_save() on the node. Feb 22, 2012 at 16:51 -
1For examples on how to implement hook_update_N() just look at the .install file of any contributed module. Contrib code will often provide a better example than the code we could come up with here. Also the original link to the hook in the question is helpful. Feb 22, 2012 at 16:53