<?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 that script must be loaded by site.com/update_cats.php?8765trfvb=54rhi9s4mmd1
// recommmended you customize the random strings
// do NOT leave on server!
if ( !isset( $_GET['8765trfvb'] ) || '54rhi9s4mmd1' != $_GET['8765trfvb'] )
{
die();
}
// Load WP instance
require_once( 'wp-config.php' );
require_once( 'wp-load.php' );
// Set vars
$limit = -1; // -1 = does all
$offset = 0; // if you've got a limit, increment your offset and re-run
/**
* PUT YOUR REASSIGNMENTS HERE
*/
$cats_to_change = array(
"Old Category Name" => "New Category Name",
);
// NO touchy. Here be dragons.
$count = 0;
foreach ( $cats_to_change as $old_cat => $new_cat )
{
// Run through our offsets
if ( 0 < $offset )
{
$offset--;
continue;
}
// 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 );
}
Wordpress: 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.
Creates any new categories that don't yet exist.
Removes old categories that have now been emptied.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.