Batch moving records (posts) from one taxonomy to another (from category to tag)
Today, a friend asked how to merge articles from two categories into one. After realizing the depth of the question, I jokingly replied to him - move each article to another category.
Of course, it was about a large number of articles and this needed to be done programmatically. Without thinking too long, I wrote this function, which moves all articles from one category to another (you can specify where from and where to):
function kama_change_cat( $change_from, $change_to ){
global $wpdb;
$sql = "
SELECT p.ID, p.post_title FROM $wpdb->posts p
JOIN $wpdb->term_relationships rel ON ( p.ID = rel.object_id )
WHERE rel.term_taxonomy_id = '$change_from'
";
$posts = $wpdb->get_results( $sql );
if( ! $posts ){
return print 'ERROR: Empty query result.';
}
echo '<ol>';
foreach( $posts as $res ){
$wpdb->query( "
UPDATE $wpdb->term_relationships
SET term_taxonomy_id = '$change_to'
WHERE object_id = '$res->ID'
" );
echo "<li>Updated record with ID: $res->ID ($res->post_title)</li>";
}
echo '</ol>';
}
Place this code somewhere, and then call the function like this:
<?php kama_change_cat( 1, 8 ); ?>
where instead of 1 and 8, you need to specify the category IDs: from (1) and to (8) to move. Before that, be sure to make a database backup! You never know...
For those who are in the dark, but need this method
-
Open the "footer.php" file, insert the function and its call at the very bottom in a row. Save "footer.php". Go to the site and refresh any page. After refreshing, at the very bottom (in the footer), you will see which records have been moved.
-
Open footer.php again and delete everything that was just added there (the function and its call).
- Enjoy

Alternate Version
To change the type of record, for example, to make posts into permanent pages or a record of a custom type, you can use the special function set_post_type().
And to move records from one taxonomy to another, you can use the function wp_set_post_terms( $post_ID, $tags, $taxonomy, $append ). Here when moving, we specify:
-
Post ID ($post_ID);
-
the names of the items of the taxonomy where we will move them ($tags);
-
taxonomy name where we will move them ($taxonomy);
- how to move: true means to leave in the current items of the taxonomy and add to the specified ones, false means to remove from the current ones and add to the specified ones ($append);
For example, let's move posts with IDs 5 and 10, which are in the "tax1" taxonomy to the "tax2" taxonomy (to the specified items of this taxonomy with IDs 45 and 5):
$posts = array(5,10);
foreach( $posts as $post_id ){
if( wp_set_post_terms( $post_id, array(45, 5), "tax2", false ) ){
echo "Moved";
}
}
Plugin
In the comments, it was suggested to use the Bulk Move plugin to move articles from one category to another or for mass deletion.