Created
October 30, 2011 14:38
-
-
Save billerickson/1325968 to your computer and use it in GitHub Desktop.
Default terms for taxonomy
This file contains hidden or 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 | |
/** | |
* 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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