Skip to content

Instantly share code, notes, and snippets.

@anthonycole
Created September 14, 2010 01:43
Show Gist options
  • Save anthonycole/578395 to your computer and use it in GitHub Desktop.
Save anthonycole/578395 to your computer and use it in GitHub Desktop.
<?php
/**
*
* @package WordPress
* @subpackage Taxonomy
*
* @uses $wpdb
*/
function set_term_taxonomy( $args ) {
global $wpdb;
$defaults = array(
'term_name' => '',
'new_taxonomy' => '',
'old_taxonomy' => '',
'children' => false,
);
wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
if( empty( $term_name ) || empty( $new_taxonomy ) || empty( $old_taxonomy ) ) {
return new WP_Error('Required terms not specified', 'required-terms-not-specified');
}
$sql = $wpdb->query( $wpdb->prepare( "UPDATE %s SET taxonomy='%s' WHERE term_id IN (SELECT term_id FROM %s WHERE slug LIKE \%%s\% AND taxonomy == %s)", $wpdb->terms_taxonomy, $new_taxonomy, $wpdb->terms, $old_taxonomy ) );
if( true === $children ) {
$children = get_term_children( get_term( $term, $new_taxonomy ) );
foreach( $children as $child ) {
$wpdb->query( $wpdb->prepare( "UPDATE %s SET taxonomy='%s' WHERE term_id == %s", $wpdb->terms_taxonomy, $newtax, $wpdb->terms, $oldtax ) );
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment