Skip to content

Instantly share code, notes, and snippets.

@mtio
Created May 17, 2016 21:49
Show Gist options
  • Select an option

  • Save mtio/f4d9f09bf9753db326ae6cc1ff84c11b to your computer and use it in GitHub Desktop.

Select an option

Save mtio/f4d9f09bf9753db326ae6cc1ff84c11b to your computer and use it in GitHub Desktop.
$cats = [
'parent',
'children',
'grandchildren'
];
$args = [];
for ($i = 0; $i < count($cats); $i++) {
if (($exists = term_exists($cats[$i], 'category')) === 0 || $exists === false ) {
if ($i > 0) {
$parentTerm = term_exists( $cats[$i -1], 'category');
if ($parentTerm !== 0 && $parentTerm !== false) {
$args['parent'] = $parentTerm['term_id'];
} else {
throw new Exception('Failed to get parent term');
}
}
$results = wp_insert_term(
$cats[$i],
'category',
$args
);
if (is_wp_error($results)) {
throw new Exception(json_encode($results->errors));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment