Skip to content

Instantly share code, notes, and snippets.

@WebSofter
Created August 19, 2024 00:10
Show Gist options
  • Save WebSofter/5e98be71a1925316cf9b2e6a5beda5c5 to your computer and use it in GitHub Desktop.
Save WebSofter/5e98be71a1925316cf9b2e6a5beda5c5 to your computer and use it in GitHub Desktop.
wordpress get id by name in hierarchical custom categories
function get_term_id_by_name_and_parent($term_name, $taxonomy, $parent_id = 0) {
// Get the term by name within the specified taxonomy
$term = get_term_by('name', $term_name, $taxonomy);
// Check if the term exists and if it has the specified parent
if ($term && $term->parent == $parent_id) {
return $term->term_id;
}
// If the term wasn't found or the parent didn't match, return null
return null;
}
$term_name = 'Автобизнес'; // Replace with the term name
$taxonomy = 'articles'; // Replace with your custom taxonomy
$parent_id = 260; // Replace with the parent term ID, or 0 for top-level terms
$term_id = get_term_id_by_name_and_parent($term_name, $taxonomy, $parent_id);
print_r($term_id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment