It doesn’t work because you use wp_insert_term
function incorrectly. Please read the codex manual page carefully and you will see that the second argument is not parent slug, but your custom taxonomy.
So your function should look like this:
create_taxonomy_record($args) {
$parent_term = 0;
if (!empty($args[2]) && ($parent_term = term_exists($args[2], $args[1])) {
$parent_term = $parent_term['term_id'];
}
$term = term_exists($args[0], $args[1]);
if ($term == 0 || $term == null) {
wp_insert_term(
$args[0], // the term
$args[1], // the taxonomy
array(
'parent' => $parent_term,
'description' => $args[3],
'slug' => $args[4],
)
);
}
else { echo "<h1>" . $args[0] . " exists in parent " . $args[1] ."</h1>";}
}
Pay attention to $args
array, now it should has 5 elements:
- Term
- Taxonomy
- Parent term slug
- Description
- Term slug
And you can use it like this:
create_taxonomy_record(array(
'Label',
'your_custom_taxonomy_name_has_to_be_here',
0,
'Label Related',
'this_tax_term_slug',
));
/* ---> Create a child */
create_taxonomy_record(array(
'Label 2',
'your_custom_taxonomy_name_has_to_be_here',
'this_tax_term_slug',
'Label Related',
'this_tax_term_slug_a_child'
));