WordPress Snippet
-
-
Save ControlledChaos/481b6e03d384c95f890eac6a3ea165e3 to your computer and use it in GitHub Desktop.
Make Default taxonomy term for Custom Post Type - WordPress
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 ccd_set_default_object_terms( $post_id, $post ) { | |
if ( 'publish' === $post->post_status ) { | |
$defaults = array( | |
'post_tag' => array( 'jdm', 'bre' ), | |
'cars' => array( 'datsun' ), | |
); | |
$taxonomies = get_object_taxonomies( $post->post_type ); | |
foreach ( (array) $taxonomies as $taxonomy ) { | |
$terms = wp_get_post_terms( $post_id, $taxonomy ); | |
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) { | |
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy ); | |
} | |
} | |
} | |
} | |
add_action( 'save_post', 'ccd_set_default_object_terms', 100, 2 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment