Title: Increased Functionality for Hierarchical Custom Post Types
Last modified: August 21, 2016

---

# Increased Functionality for Hierarchical Custom Post Types

 *  Resolved [dfenton](https://wordpress.org/support/users/dfenton/)
 * (@dfenton)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/increased-functionality-for-hierarchical-custom-post-types/)
 * I was having issues with uploading to a custom taxonomy with hierarchical structure
   because the current code only supports a single layer of categories (i.e. one
   parent and one child), whereas I had a category structure that needed as many
   as 4 layers (great-grandparent, grandparent, parent, and finally children). If
   anyone else has this problem just replace the current create_terms function in
   csv_importer.php with the code below. This will allow you upload as many parent
   layers as you need.
 * If there are issues or problems let me know and I’ll check it out. I’m not a 
   pro and I just edited the existing function so it’s possible that this could 
   break your site somehow, use at your own peril and don’t blame me!
 * It works fine on the couple sites where I’ve used it, but again, it’s not tested
   or noob-proof.
 *     ```
       function create_terms($taxonomy, $field) {
       		if (is_taxonomy_hierarchical($taxonomy)) {
       			$term_ids = array();
       			foreach ($this->_parse_tax($field) as $row) {
       				$num_terms = count($row);
       				$i = 0;
       				$parent_id = false;
       				while ($i < $num_terms){
       					if($i == 0){
       						$term_info = $this->term_exists($row[$i], $taxonomy);
       						if(!$term_info){
       							//echo 'first term does not exist, need to create it<br>';
       							$term_info = wp_insert_term($row[$i], $taxonomy);
       						}
       						if($term_info){
       							//echo 'first term exists or returned an error<br>';
       							if (!is_wp_error($term_info)) {
       								$parent_id = $term_info['term_id'];
       								//echo 'term did not error, current parent id is: '.$parent_id.'<br>';
       							}else{
       								//echo 'term registered an error<br>';
       								$parent_id = false;
       							}
       						}
       					}else if($i == $num_terms-1){
       						$term_info = $this->term_exists($row[$i], $taxonomy, $parent_id);
       						if(!$term_info){
       							//echo 'last term does not exist, need to create it<br>';
       							$term_info = wp_insert_term($row[$i], $taxonomy, array('parent' => $parent_id));
       						}
       						if($term_info){
       							//echo 'last term exists or returned an error<br>';
       							if (!is_wp_error($term_info)) {
       								$term_ids[] = $term_info['term_id'];
       								//echo 'final term id set to: '.$term_info['term_id'].'<br>';
       							}
       						}
       					}else{
       						$term_info = $this->term_exists($row[$i], $taxonomy, $parent_id);
       						if(!$term_info){
       							//echo 'middle term does not exist, need to create it<br>';
       							$term_info = wp_insert_term($row[$i], $taxonomy, array('parent' => $parent_id));
       						}
       						if($term_info){
       							//echo 'middle term exists or returned an error<br>';
       							if (!is_wp_error($term_info)) {
       								$parent_id = $term_info['term_id'];
       								//echo 'term did not error, current parent id is: '.$parent_id.'<br>';
       							}else{
       								//echo 'term registered an error<br>';
       								$parent_id = false;
       							}
       						}
       					}
       					$i+= 1;
       				}
       			}
       			//echo 'term ids returned are: ';
       			//print_r( $term_ids );
       			return $term_ids;
       		} else {
       			return $field;
       		}
       	}
       ```
   
 * [https://wordpress.org/plugins/csv-importer/](https://wordpress.org/plugins/csv-importer/)

The topic ‘Increased Functionality for Hierarchical Custom Post Types’ is closed
to new replies.

 * ![](https://ps.w.org/csv-importer/assets/icon-256x256.png?rev=3039593)
 * [CSV Importer](https://wordpress.org/plugins/csv-importer/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/csv-importer/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/csv-importer/)
 * [Active Topics](https://wordpress.org/support/plugin/csv-importer/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/csv-importer/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/csv-importer/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [dfenton](https://wordpress.org/support/users/dfenton/)
 * Last activity: [12 years, 2 months ago](https://wordpress.org/support/topic/increased-functionality-for-hierarchical-custom-post-types/)
 * Status: resolved