Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created October 30, 2011 14:38
Show Gist options
  • Save billerickson/1325968 to your computer and use it in GitHub Desktop.
Save billerickson/1325968 to your computer and use it in GitHub Desktop.
Default terms for taxonomy
<?php
/**
 * Define default terms for custom taxonomies
 *
 * @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(
'poc' => array( 'email-form' ),
);
$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 );
@andersonnarciso
Copy link

Hi man..

'poc' => array( 'email-form' ),

poc is taxonomy and email-form is term?

Exemple

'city' => array( 'seattle' ),

Because dont work for me, Seattle is not marked by default to create a new post

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