Skip to content

Instantly share code, notes, and snippets.

@sarahbayley
Last active March 7, 2021 06:49
Show Gist options
  • Save sarahbayley/c7fe0f40491d2a28a026d8c6f6cf6a94 to your computer and use it in GitHub Desktop.
Save sarahbayley/c7fe0f40491d2a28a026d8c6f6cf6a94 to your computer and use it in GitHub Desktop.
Copy WP (child) terms to another taxonomy. Example copies children of term with ID '32' in the taxonomy 'category' (i.e. all my Vocab subcategories) to the custom taxonomy 'sets'. Use in functions.php.
$term_id = 32;
$from_taxonomy = 'category';
$to_taxonomy = 'sets';
$termchildren = get_term_children( $term_id, $from_taxonomy );
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $from_taxonomy );
$name = $term->name;
// if the term doesn't already exist in the target category...
if ( ! term_exists( $name, $to_taxonomy ) ) {
// ...insert term
add_action('init', function() use($name,$to_taxonomy) {
wp_insert_term($name, $to_taxonomy);
}, 10, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment