Skip to content

Instantly share code, notes, and snippets.

@ControlledChaos
Forked from mayeenulislam/Default Taxonomy Term for CPT
Last active August 8, 2017 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ControlledChaos/481b6e03d384c95f890eac6a3ea165e3 to your computer and use it in GitHub Desktop.
Save ControlledChaos/481b6e03d384c95f890eac6a3ea165e3 to your computer and use it in GitHub Desktop.
Make Default taxonomy term for Custom Post Type - WordPress

Default Taxonomy Term

WordPress Snippet

<?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