Advertisement
Guest User

auto_create_campaigns()

a guest
Jul 12th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1. add_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
  2. function auto_create_campaigns( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
  3.     if ( 'campaigns' !== get_post_type( $object_id ) || 'country' !== $taxonomy ) {
  4.         return;
  5.     }
  6.  
  7.     if ( $append ) {
  8.         $term_ids = $tt_ids;
  9.     } else {
  10.         $term_ids = array_diff( $tt_ids, $old_tt_ids );
  11.     }
  12.  
  13.     $post = get_post( $object_id, ARRAY_A );
  14.     unset( $post['ID'] );
  15.  
  16.     foreach ( $term_ids as $term_id ) {
  17.         $term = get_term( $term_id, $taxonomy );
  18.         if ( is_wp_error( $term ) || empty( $term ) ) {
  19.             continue;
  20.         }
  21.  
  22.         $post['post_title'] = $term->name;
  23.         $post['post_name'] = $term->slug;
  24.         $post['post_parent'] = $object_id;
  25.  
  26.         $post_id = wp_insert_post( $post );
  27.         error_log( sprintf( 'Post%s created.%s',
  28.             $post_id ? '' : ' not',
  29.             $post_id ? ' ID: ' . $post_id : ''
  30.         ) );
  31.  
  32.         // Set post terms.
  33.         remove_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
  34.         /*if ( $post_id ) {
  35.             // Copy the parent's "countries".
  36.             // wp_set_post_terms( $post_id, $terms, $taxonomy );
  37.         }*/
  38.         add_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
  39.  
  40.         /*if ( $post_id ) {
  41.             // Copy the parent's custom fields.
  42.         }*/
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement