0
votes

Is there a way, if you publish a post without checking a taxonomy, to control which term it defaults to?

I have a taxonomy called posttypes and terms: downloads, links and blog-post. I need it to default to blog-post if the post is published without a checked term. It defaults to downloads.

I tried using this:

http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/

/**
 * 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/
 * @props     Evan Mulins        http://circlecube.com/
 *
 * @since     2010-09-13
 * @alter     2013-01-31
 *
 * @license   GPLv2
 */
function mfields_set_default_object_terms( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'posttypes' => array( 'blog-post' )
            );
        $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 );

But it doesn't seem to be working.

1
This question appears to be off-topic because it is about WordPress, belongs to wordpress.stackexchange.comRaptor
Did you remember to use 'add_action'?Nathan Dawson
Forgot to copy that in.user1272433

1 Answers

-1
votes
function mfields_set_default_object_terms( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'posttypes' => array( 'blog-post' )