Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
- function auto_create_campaigns( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
- if ( 'campaigns' !== get_post_type( $object_id ) || 'country' !== $taxonomy ) {
- return;
- }
- if ( $append ) {
- $term_ids = $tt_ids;
- } else {
- $term_ids = array_diff( $tt_ids, $old_tt_ids );
- }
- $post = get_post( $object_id, ARRAY_A );
- unset( $post['ID'] );
- foreach ( $term_ids as $term_id ) {
- $term = get_term( $term_id, $taxonomy );
- if ( is_wp_error( $term ) || empty( $term ) ) {
- continue;
- }
- $post['post_title'] = $term->name;
- $post['post_name'] = $term->slug;
- $post['post_parent'] = $object_id;
- $post_id = wp_insert_post( $post );
- error_log( sprintf( 'Post%s created.%s',
- $post_id ? '' : ' not',
- $post_id ? ' ID: ' . $post_id : ''
- ) );
- // Set post terms.
- remove_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
- /*if ( $post_id ) {
- // Copy the parent's "countries".
- // wp_set_post_terms( $post_id, $terms, $taxonomy );
- }*/
- add_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
- /*if ( $post_id ) {
- // Copy the parent's custom fields.
- }*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement