lolitaloco

check terms ancestry on post save (wordpress)

Apr 9th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.50 KB | None | 0 0
  1. /* IF A CHILD TERM IS SET, ALSO SET PARENT TERM (TAXONOMY NAME 'SEKTION' )*/
  2. function check_terms_ancestry($post_id) {
  3.     if(!wp_is_post_revision($post_id)) {
  4.         $terms_parents = array();
  5.         $terms = wp_get_post_terms($post_id, 'sektion');
  6.         foreach ($terms as $term) {
  7.             array_push($terms_parents, $term->parent);
  8.         }
  9.         if($terms_parents) {
  10.             array_unique($terms_parents);      
  11.             wp_set_post_terms($post_id, $terms_parents, 'sektion', TRUE);
  12.         }
  13.     }
  14. }
  15. add_action("save_post", "check_terms_ancestry", 10, 2);
Add Comment
Please, Sign In to add comment