Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $location_array_reversed = array_reverse( $location_array );
- $i = 0;
- $len = count($location_array_reversed);
- $location_array_ids = array();
- foreach( $location_array_reversed as $term ){
- if ($i == 0) {
- // Top level term
- wp_insert_term( $term, 'location' );
- $tag = get_term_by( 'slug', $term, 'location' );
- $term_id = $tag->term_id;
- // Save term ID to array
- $location_array_ids[] = $term_id;
- } else if ($i == $len - 1) {
- wp_insert_term( $term, 'location', array( 'parent'=> $term_id ) );
- // Child terms
- wp_insert_term( $term, 'location' );
- $tag = get_term_by( 'slug', $term, 'location' );
- $term_id = $tag->term_id;
- // Save term ID to array
- $location_array_ids[] = $term_id;
- }
- $i++;
- }
- // Now assign terms to post
- wp_set_object_terms( $post_id, $location_array_ids, 'location' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement