@@ -82,16 +82,28 @@ protected static function generate_term_ids( $limit, $taxonomy, $name = '' ) {
8282 $ args = array ( 'parent ' => 0 );
8383
8484 foreach ( $ terms as $ term ) {
85- if ( isset ( self ::$ term_ids [ $ taxonomy ], self ::$ term_ids [ $ taxonomy ][ $ term ] ) ) {
86- $ term_ids [] = self ::$ term_ids [ $ taxonomy ][ $ term ];
85+ if ( isset ( self ::$ term_ids [ $ taxonomy ][ $ args ['parent ' ] ], self ::$ term_ids [ $ taxonomy ][ $ args ['parent ' ] ][ $ term ] ) ) {
86+ $ term_id = self ::$ term_ids [ $ taxonomy ][ $ args ['parent ' ] ][ $ term ];
87+ $ term_ids [] = $ term_id ;
88+
89+ if ( is_taxonomy_hierarchical ( $ taxonomy ) ) {
90+ $ args ['parent ' ] = $ term_id ;
91+ }
92+
8793 continue ;
8894 }
8995
90- $ term_id = 0 ;
91- $ existing = get_term_by ( 'name ' , $ term , $ taxonomy );
96+ $ term_id = 0 ;
97+ $ args = array (
98+ 'taxonomy ' => $ taxonomy ,
99+ 'name ' => $ term ,
100+ 'parent ' => $ args ['parent ' ],
101+ );
102+
103+ $ existing = get_terms ( $ args );
92104
93- if ( $ existing && ! is_wp_error ( $ existing ) ) {
94- $ term_id = $ existing ->term_id ;
105+ if ( $ existing && count ( $ existing ) && ! is_wp_error ( $ existing ) ) {
106+ $ term_id = $ existing[ 0 ] ->term_id ;
95107 } else {
96108 $ term_ob = wp_insert_term ( $ term , $ taxonomy , $ args );
97109
@@ -101,9 +113,13 @@ protected static function generate_term_ids( $limit, $taxonomy, $name = '' ) {
101113 }
102114
103115 if ( $ term_id ) {
104- $ term_ids [] = $ term_id ;
105- self ::$ term_ids [ $ taxonomy ][ $ term ] = $ term_id ;
106- $ args ['parent ' ] = $ term_id ;
116+
117+ $ term_ids [] = $ term_id ;
118+ self ::$ term_ids [ $ taxonomy ][ $ args ['parent ' ] ][ $ term ] = $ term_id ;
119+
120+ if ( is_taxonomy_hierarchical ( $ taxonomy ) ) {
121+ $ args ['parent ' ] = $ term_id ;
122+ }
107123 }
108124 }
109125
0 commit comments