Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created September 8, 2015 05:54
Show Gist options
  • Select an option

  • Save Ciantic/b6ef49f0358bf913f394 to your computer and use it in GitHub Desktop.

Select an option

Save Ciantic/b6ef49f0358bf913f394 to your computer and use it in GitHub Desktop.
Insert or Get Term ID
<?php
/**
* (WordPress) Insert or get term id
*
* @return int Term ID (0 no term was inserted or found)
*/
function __wp_insert_or_get_term_id($name, $taxonomy, $parent = 0) {
if (!($term = get_term_by("name", $name, $taxonomy))) {
$insert = wp_insert_term($name, $taxonomy, array(
"parent" => $parent
));
if (is_wp_error($insert)) {
return 0;
}
return intval($insert["term_id"]);
}
return intval($term->term_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment