Last active
July 14, 2020 17:24
-
-
Save cabrailsford/b15b1c968f84ec220d14f15b95a3b14e to your computer and use it in GitHub Desktop.
WordPress function to create or update custom taxonomy terms from an ACF date-picker on post save.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function create_manga_categories( $post_id ) { | |
$date = get_field('manga_group')['manga_release_date']; | |
if(get_post_type( $post_id ) == 'manga' && $date) { | |
$date_convert = DateTime::createFromFormat('Ymd', $date); | |
$date_converted = $date_convert->format('F Y'); | |
$date_converted2 = $date_convert->format('F j'); | |
$date_converted3 = $date_convert->format('F-j-Y'); | |
$terms = []; | |
$parent_term_check = get_term_by('name', $date_converted, 'manga_release_date', 'ARRAY_A'); | |
if(!$parent_term_check) { | |
$parent_term = wp_insert_term($date_converted, 'manga_release_date'); | |
$parent_term_id = $parent_term['term_id']; | |
} else { | |
$parent_term_id = $parent_term_check['term_id']; | |
} | |
$terms[] = $parent_term_id; | |
$child_term_check = get_term_by('slug', $date_converted3, 'manga_release_date', 'ARRAY_A'); | |
if(!$child_term_check) { | |
$child_term = wp_insert_term($date_converted2, 'manga_release_date', array('parent'=>$parent_term_id, 'slug'=>$date_converted3)); | |
$terms[] = $child_term['term_id']; | |
} else { | |
$terms[] = $child_term_check['term_id']; | |
} | |
wp_set_object_terms( $post_id, $terms, 'manga_release_date' ); | |
} | |
elseif( get_post_type( $post_id) == 'manga' ) { | |
wp_set_object_terms( $post_id, null, 'manga_release_date'); | |
} | |
} | |
add_action('acf/save_post', 'create_manga_categories', 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment