Skip to content

Instantly share code, notes, and snippets.

@getneerajk
Created November 8, 2023 13:07
Show Gist options
  • Save getneerajk/f418c67be6039eebabed10cca8a88281 to your computer and use it in GitHub Desktop.
Save getneerajk/f418c67be6039eebabed10cca8a88281 to your computer and use it in GitHub Desktop.
Synchronize terms between the two taxonomies #taxonomy #wp
<?php
// Synchronize terms between the two taxonomies
function synchronize_taxonomies( $term_id, $tt_id, $taxonomy ) {
if (in_array($taxonomy, array('first_taxonomy', 'second_taxonomy'))) {
$term = get_term($term_id, $taxonomy);
$other_taxonomy = ($taxonomy === 'first_taxonomy') ? 'second_taxonomy' : 'first_taxonomy';
// Check if the term exists in the other taxonomy
$term_exists = term_exists($term->name, $other_taxonomy);
// If the term doesn't exist in the other taxonomy, create it
if (!$term_exists) {
wp_insert_term($term->name, $other_taxonomy, array(
'description' => $term->description,
'slug' => $term->slug,
));
}
}
}
add_action('create_term', 'synchronize_taxonomies', 10, 3);
add_action('edit_term', 'synchronize_taxonomies', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment