Skip to content

Instantly share code, notes, and snippets.

@getneerajk
Created December 30, 2024 05:00
Show Gist options
  • Save getneerajk/526ebe7eb820fbfbed9197af0312749a to your computer and use it in GitHub Desktop.
Save getneerajk/526ebe7eb820fbfbed9197af0312749a to your computer and use it in GitHub Desktop.
2 terms with same slug #term #wp
<?php
/**
* Get terms with the same slug in two different taxonomies.
*
* @param string $taxonomy_1 The first taxonomy.
* @param string $taxonomy_2 The second taxonomy.
* @return array An associative array where the key is the term ID of taxonomy_1 and the value is the term ID of taxonomy_2.
*/
function get_terms_with_same_slug($taxonomy_1, $taxonomy_2) {
$terms_taxonomy_1 = get_terms([
'taxonomy' => $taxonomy_1,
'hide_empty' => false,
]);
$terms_taxonomy_2 = get_terms([
'taxonomy' => $taxonomy_2,
'hide_empty' => false,
]);
if (is_wp_error($terms_taxonomy_1) || is_wp_error($terms_taxonomy_2)) {
return [];
}
$taxonomy_2_slugs = [];
foreach ($terms_taxonomy_2 as $term) {
$taxonomy_2_slugs[$term->slug] = $term->term_id;
}
$result = [];
foreach ($terms_taxonomy_1 as $term) {
if (isset($taxonomy_2_slugs[$term->slug])) {
$result[$term->term_id] = $taxonomy_2_slugs[$term->slug];
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment