Anybody got any solutions for this...

I'm building an app where I have two heirchical custom taxnomies acting as custom categories. So i've got cat1 and cat2 taxonomy.

What I want to do is copy a term, along with its children from cat1 to cat2. Maybe something like:

set_term_taxonomy($term_id, array("cat1, "cat2"));

where the same term, along with its children, could be in more than one taxonomy. The reason for 2 taxonomies is that they are 2 different shop inventories.

  • 1
    so far all I can think of is writing a routine to loop through a term and its children, then recreate each term in the new taxonomy, giving the effect of it being copied across
    – Daithí
    Commented Oct 19, 2011 at 15:16
  • 1
    keep in mind that slug is a unique key in the wp_terms table, you can't have two taxonomies share the same slug.
    – Milo
    Commented Oct 19, 2011 at 15:39
  • @Milo yeh and there lies the wall. I'm going to have to write a function to loop through the ancestry. I'll post it back here when its done. Some one might find it usefull ;)
    – Daithí
    Commented Oct 19, 2011 at 20:01

1 Answer 1


Had to write a method to do it. Here is the method... hope it helps someone else ;)


copy_terms($term->term_id, "taxonomy1", "taxonomy2", 0); //will copy to root of destination

 * Copy a term and its descendants from one taxonomy to another.
 * Both taxonomies have to be hierarchical. Will copy across 
 * posts as well.
 * @param int $term the term id to copy
 * @param string $from the taxonomy of the original term
 * @param string $to the destination for the taxonomy
 * @param int $parent the parent term_id to add the taxonomy to
 * @return type true|WP_Error
function copy_terms($term, $from, $to, $parent=0) {

    //check for child terms      
    $term = get_term($term, $from);
    $child_terms = get_terms($from, array(
        'hide_empty' => false,
        'parent' => $term->term_id

    //check for child products
    $child_prods = new WP_Query(array(
            'tax_query' => array(
                    'taxonomy' => $from,
                    'terms' => $term->term_id

    //work out new slug
    if($parent==0) $slug = $to."-".sanitize_title($term->name);
        $parent_term = get_term($parent, $to);
        $slug = $parent_term->slug."-".sanitize_title($term->name);

    //add term to new taxonomy
    $parent = wp_insert_term($term->name, $to, array(
        'description' => $term->description,
        'parent' => $parent,
        'slug' => $slug

    if(is_wp_error($parent)) return $parent;
    $parent = get_term($parent['term_id'], $to);        
    if(is_wp_error($parent)) return $parent;

    //loop through folders first
    foreach($child_terms as $child){
        copy_terms($child->term_id, $from, $to, $parent->term_id);

    //loop through products
    foreach($child_prods->posts as $child){
        wp_set_post_terms($child->ID, $parent->term_id, $to, true);
    return true;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.