必须编写一个方法来完成。以下是方法。。。希望它能帮助其他人;)
语法:
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(
array(
\'taxonomy\' => $from,
\'terms\' => $term->term_id
)
)
));
//work out new slug
if($parent==0) $slug = $to."-".sanitize_title($term->name);
else{
$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;