Created
May 17, 2016 21:49
-
-
Save mtio/f4d9f09bf9753db326ae6cc1ff84c11b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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