Advertisement
parkeast

Set Taxonomy of Post Author in Gravity Forms

Jul 6th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1.  
  2. // SET TAXONOMY OF AUTHOR
  3.  
  4. add_action('gform_post_submission', 'ounce_gform_post_submission', 10, 2);
  5. function ounce_gform_post_submission($entry, $form) {
  6.    
  7. // if no post was created, return
  8.     if(!$entry['post_id'])
  9.         return;
  10.    
  11. // name_ID = the ID of the author in the author custom taxonomy
  12.     if ($entry["1"] == 'Poe') {
  13.         $name_ID  = "12";
  14.     } elseif ($entry["1"] == 'King') {
  15.         $name_ID  = "24";
  16.     } elseif ($entry["1"] == 'Lewis') {
  17.         $name_ID  = "48";
  18.     } elseif ($entry["1"] == 'Tolkien') {
  19.         $name_ID  = "96";
  20.     }
  21.     $term_id = $name_ID;    
  22.  
  23. // the slug name of the taxonomy you set up
  24.     $taxonomy = 'author';
  25.    
  26.  
  27. // if we have a taxonomy and a field id, add term to post
  28.     if($taxonomy && $term_id)
  29.         ounce_add_term_to_post($taxonomy, $term_id, $entry['post_id']);
  30. }
  31.  
  32. // add term from taxonomy to post
  33. function ounce_add_term_to_post($taxonomy = "categories", $term_id, $post_id) {
  34.  
  35.     $terms = get_terms($taxonomy, array('hide_empty' => 0));
  36.    
  37.     foreach($terms as $term) {
  38.        
  39.         if($term->term_id == $term_id)
  40.             $result = wp_set_object_terms($post_id, (int) $term_id, $taxonomy, false);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement