Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Last active April 5, 2023 11:07
Show Gist options
  • Save mayeenulislam/f208b4fd408fd4742c06 to your computer and use it in GitHub Desktop.
Save mayeenulislam/f208b4fd408fd4742c06 to your computer and use it in GitHub Desktop.
Make Default taxonomy term for Custom Post Type - WordPress
/**
* Author: Michael Fields
* Source: http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/
* Thanks a lot for the nice tweak
*/
/**
* Define default terms for custom taxonomies in WordPress 3.0.1
*
* @author Michael Fields http://wordpress.mfields.org/
* @props John P. Bloch http://www.johnpbloch.com/
*
* @since 2010-09-13
* @alter 2010-09-14
*
* @license GPLv2
*/
function mfields_set_default_object_terms( $post_id, $post ) {
if ( 'publish' === $post->post_status ) {
$defaults = array(
'post_tag' => array( 'taco', 'banana' ),
'monkey-faces' => array( 'see-no-evil' ),
);
$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', 'mfields_set_default_object_terms', 100, 2 );
/**
* Just change the 'post_tag' with the taxonomy slug you want to target
* and change 'taco' and 'banana' with the slug of the term you want to make default
* you can add multiple taxonomy at once so the line#19 is applicable only then
*/
@OksanaRomaniv
Copy link

Thank you for the snippet! For anyone who is looking to modify custom post type, you can use the hook 'save_post_{$post_type}', e.g. 'save_post_event', where 'event' is an example slug of your Custom Post Type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment