Last active
August 29, 2015 14:18
-
-
Save Blindmikey/f76eb2ac5072dff26fb3 to your computer and use it in GitHub Desktop.
Simple script to update/reassign Wordpress posts from old categories to new categories.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simple script to update/reassign posts from old categories to new categories. | |
* Creates any new categories that don't yet exist. | |
* Removes old categories that have now been emptied. | |
* | |
* @author Michael Niles <blindmikey@gmail.com> | |
* @version 1.0.0 | |
*/ | |
// rudimentary security | |
// requires script must be loaded by site.com/update_cats.php?8765trfvb=54rhi9s4mmd1 | |
if ( !isset($_GET['8765trfvb']) || '54rhi9s4mmd1' != $_GET['8765trfvb'] ) { | |
die(); | |
} | |
// Load WP instance | |
require_once('wp-config.php'); | |
require_once('wp-load.php'); | |
global $wpdb; | |
// Set vars | |
$limit = -1; // -1 = does all | |
$cats_to_change = array( | |
"Old Category Name" => "New Category Name", // Put your reassignments here | |
); | |
$count = 0; | |
foreach ( $cats_to_change as $old_cat => $new_cat ) | |
{ | |
// If we're past our limit then stop | |
$count++; | |
if ( $limit < $count && 0 <= $limit ) | |
{ | |
echo __LINE__.': Reassignment limit reached. Stopping.<br/>'.PHP_EOL; | |
break; | |
} | |
$old_cat_id = get_cat_ID( $old_cat ); | |
$new_cat_id = get_cat_ID( $new_cat ); | |
// If the old cat is not found, skip | |
if ( ! $old_cat_id ) | |
{ | |
echo __LINE__.': Old category NOT found: "'.$old_cat.'"<br/>'.PHP_EOL; | |
echo __LINE__.': Skipping Reassignment for: "'.$old_cat.'"<br/>'.PHP_EOL; | |
continue; | |
} | |
// If the new cat is the same as the old cat, skip | |
if ( $new_cat_id == $old_cat_id ) | |
{ | |
echo __LINE__.': New category already exists as Old category: "'.$new_cat.'" = "'.$old_cat.'"<br/>'.PHP_EOL; | |
echo __LINE__.': Skipping Reassignment for: "'.$old_cat.'"<br/>'.PHP_EOL; | |
continue; | |
} | |
// Create New Category | |
if ( ! $new_cat_id ) | |
{ | |
echo __LINE__.': New category NOT found: "'.$new_cat.'"<br/>'.PHP_EOL; | |
echo __LINE__.': Creating category for: "'.$new_cat.'"<br/>'.PHP_EOL; | |
$inserted_term = wp_create_category( $new_cat ); | |
if ( 0 == $inserted_term ) | |
{ | |
echo __LINE__.': Failure. Could NOT create new category. Skipping.<br/>'.PHP_EOL; | |
continue; | |
} | |
else | |
{ | |
$new_cat_id = $inserted_term; | |
echo __LINE__.': Success. Created as cat id: "'.$new_cat_id.'"<br/>'.PHP_EOL; | |
} | |
} | |
else { | |
echo __LINE__.': New category already exists as: "'.$new_cat.'"<br/>'.PHP_EOL; | |
} | |
/** | |
* Get Posts by Old Category | |
*/ | |
$posts = get_posts(array( | |
'posts_per_page' => -1, | |
'category' => $old_cat_id | |
)); | |
foreach ( $posts as $post ) | |
{ | |
$post_id = $post->ID; | |
$cat_ids = wp_get_post_categories( $post_id ); | |
echo __LINE__.': Reassigning categories for post ID: '.$post_id.'<br/>'.PHP_EOL; | |
// remove old category | |
if(($key = array_search($old_cat_id, $cat_ids)) !== false) { | |
echo __LINE__.': Removing old category: "'.$old_cat.'" / ID: '.$old_cat_id.'<br/>'.PHP_EOL; | |
unset($cat_ids[$key]); | |
} | |
// add our new category | |
echo __LINE__.': Adding new category: "'.$new_cat.'" / ID: '.$new_cat_id.'<br/>'.PHP_EOL; | |
$cat_ids[] = $new_cat_id; | |
// prepare our cat_ids | |
$cat_ids = array_map( 'intval', $cat_ids ); | |
$cat_ids = array_unique( $cat_ids ); | |
// set the post to use only these cat_ids | |
wp_set_object_terms( $post_id, $cat_ids, 'category' ); | |
} | |
// we're done with the old - remove it. | |
echo __LINE__.': Deleting old category ID: '.$old_cat_id.'<br/>'.PHP_EOL; | |
wp_delete_category( $old_cat_id ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment