Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. $location_array_reversed = array_reverse( $location_array );
  2.  
  3. $i = 0;
  4. $len = count($location_array_reversed);
  5.  
  6. $location_array_ids = array();
  7.  
  8. foreach( $location_array_reversed as $term ){
  9.  
  10. if ($i == 0) {
  11.  
  12. // Top level term
  13. wp_insert_term( $term, 'location' );
  14. $tag = get_term_by( 'slug', $term, 'location' );
  15. $term_id = $tag->term_id;
  16.  
  17. // Save term ID to array
  18. $location_array_ids[] = $term_id;
  19.  
  20. } else if ($i == $len - 1) {
  21.  
  22. wp_insert_term( $term, 'location', array( 'parent'=> $term_id ) );
  23.  
  24. // Child terms
  25. wp_insert_term( $term, 'location' );
  26. $tag = get_term_by( 'slug', $term, 'location' );
  27. $term_id = $tag->term_id;
  28.  
  29. // Save term ID to array
  30. $location_array_ids[] = $term_id;
  31.  
  32. }
  33.  
  34. $i++;
  35.  
  36. }
  37.  
  38. // Now assign terms to post
  39. wp_set_object_terms( $post_id, $location_array_ids, 'location' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement