Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Wordpress default taxonomy (Categories) has the item Uncategorized by default. How to add a default item to a new custom taxonomy?

functions.php:

// === CUSTOM TAXONOMIES === //
function my_custom_taxonomies() {
    register_taxonomy(
        'block',        // internal name = machine-readable taxonomy name
        'static_content',       // object type = post, page, link, or custom post-type
        array(
            'hierarchical' => true,
            'labels' => array(
                'name' => __( 'Blocks' ),
                'singular_name' => __( 'Block' ),
                'add_new_item' => 'Add New Block',
                'edit_item' => 'Edit Block',
                'new_item' => 'New Block',
                'search_items' => 'Search Block',
                'not_found' => 'No Block found',
                'not_found_in_trash' => 'No Block found in trash',
            ),
            'query_var' => true,    // enable taxonomy-specific querying
            'rewrite' => array( 'slug' => 'block' ),    // pretty permalinks for your taxonomy?
        )
    );
}
add_action('init', 'my_custom_taxonomies', 0);

EDIT: I just want to have the taxonomy item there when the theme is installed. It doesn't have to automatically be added to any empty term.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

Have a look here:

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

Basically what you need to do is use the save_post hook to check the terms for the post and add the default term from your taxonomy if it's empty.

If you just want to have an initial term set in your custom taxonomy, then you can use wp_insert_term(). Probably easiest to add it in the same function that you're using to create your custom taxonomy. As t3ios adds in the comments, you should call get_term() first, and only insert the term if the return value is null (ie the term doesn't exist).

This example code is from the Codex: http://codex.wordpress.org/Function_Reference/wp_insert_term

$parent_term = term_exists( 'fruits', 'product' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term['term_id']; // get numeric term id
wp_insert_term(
  'Apple', // the term 
  'product', // the taxonomy
  array(
    'description'=> 'A yummy apple.'
    'slug' => 'apple'
    'parent'=> $parent_term_id
  )
);
share|improve this answer
    
@anu I think I didn't explain myself very well, I just wanted to have that taxonomy there when the theme is installed. It doesn't have to any term if it is empty. –  janoChen Jan 18 '11 at 12:00
    
@janoChen - I updated the answer –  anu Jan 18 '11 at 12:09
    
@anu Excellent, I wish I could vote your answer more. I edited the questions. Where exactly should I place that code you wrote above? –  janoChen Jan 18 '11 at 12:13
    
The code I added is just an example (and it's from the WordPress codex) - so you'll need to amend it as necessary. Best place to add it would be just before the closing brace for the function. –  anu Jan 18 '11 at 12:15
1  
You sure you want to run the insert in that function, it runs on init, ie. every page, i don't think you want to run the insert every time a page is called do you? Maybe the insert function returns null/false if a matching term is already existing with that name(i havn't looked), but all the same that seems a little needless(why not call get_term(s) and see if it exists, then insert if not). –  t31os Jan 18 '11 at 16:30

Default category is hardcoded case in wp_insert_post() function.

So it can't be replicated exactly, but you can handle it in other ways. I'd try to hook into post status transition for new posts and assign wanted default term if none were assigned during post creation.

share|improve this answer

I needed to populate a Custom Taxonomy "Days" with the days of the week..I did not want the client to have to mess with creating days, or get in there and delete days or misspell days. Following the above advice I came up with this, but I am wondering if there is a more concise way of coding it:

 /*************************************** ...Create a Custom Taxonomy for days ******************************/



add_action( 'init', 'build_taxonomies', 0 );  

function build_taxonomies() {  



    register_taxonomy( 

    'days', 

    'schedule',

   array( 'hierarchical' => true, 

    'label' => 'Days',

    'query_var' => true, 

    'show_ui' => false,     //removes the menus from admin menu and edit panel  

    'rewrite' => true ) );  



/*---------------------------------------Check to see if the days are created..if not, create them----*/



$parent_term = term_exists( 'days', 'days' ); // array is returned if taxonomy is given

$parent_term_id = $parent_term['term_id']; // get numeric term id

wp_insert_term(                            //this should probably be an array, but I kept getting errors..

        'Monday', // the term 

        'days', // the taxonomy

        array(

            'slug' => 'monday',

            'parent'=> $parent_term_id ));

wp_insert_term(

        'Tuesday', // the term 

        'days', // the taxonomy

        array(

            'slug' => 'tuesday',

            'parent'=> $parent_term_id ));

wp_insert_term(

        'Wednesday', // the term 

        'days', // the taxonomy

        array(

            'slug' => 'wednesday',

            'parent'=> $parent_term_id ));

wp_insert_term(

        'Thursday', // the term 

        'days', // the taxonomy

        array(

            'slug' => 'thursday',

            'parent'=> $parent_term_id ));

wp_insert_term(

        'Friday', // the term 

        'days', // the taxonomy

        array(

            'slug' => 'friday',

            'parent'=> $parent_term_id ));



wp_insert_term(

        'Saturday', // the term 

        'days', // the taxonomy

        array(

            'slug' => 'saturday',

            'parent'=> $parent_term_id ));



wp_insert_term(

        'Sunday', // the term 

        'days', // the taxonomy

        array(

            'slug' => 'sunday',

            'parent'=> $parent_term_id ));



}
/************ now I add my own meta box for days to get rid of extra controls *************/



add_action('admin_menu', 'add_custom_categories_box');

function add_custom_categories_box() {

 add_meta_box('myrelateddiv', 'Days*', 'ilc_post_related_meta_box', 'schedule', 'normal', 'low', array( 'taxonomy' => 'days' ));

}



function ilc_post_related_meta_box( $post, $box ) {

    $defaults = array('taxonomy' => 'related');

    if ( !isset($box['args']) || !is_array($box['args']) )

        $args = array();

    else

        $args = $box['args'];

    extract( wp_parse_args($args, $defaults), EXTR_SKIP );

    $tax = get_taxonomy($taxonomy);

    ?>

<ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">

<?php

wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy, 'popular_cats' => $popular_ids, 'checked_ontop' => FALSE ) )

?>

</ul>   
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.