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.

I'm writing some code that makes extensive use of custom post types- and I'm looking for a means of programatically adding a category to a defined custom post type, and then accessing its category ID.

I had a poke around but I can't seem to find a robust means of achieving this - wp_create_category would be the obvious choice but of course this doesn't support custom post types.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

if you need to pre-create some terms you can add the following into the init function that registers the taxonomy. it will create the term foo in the recordings taxonomy

if (!term_exists( 'foo', 'recordings') ){
        wp_insert_term( 'foo', 'recordings' );
    })
share|improve this answer
    
That's perfect! Is there any way of getting the ID of the created term? I didn't word my question especially well so many thanks. –  George Pearce Apr 2 '12 at 20:44
    
well wp_insert_term returns the term and taxonomy IDs see: codex.wordpress.org/Function_Reference/… you could also use get_term_by : codex.wordpress.org/Function_Reference/get_term_by –  helgatheviking Apr 2 '12 at 21:20

Inside your register_post_type you can add the taxonomies parameter.

register_post_type('discography',
    array(
    'labels' => array(
             //your label stuff
             ),

  'taxonomies' => array('recordings', 'category', 'whatever'),  //add this....

  'public' => true,
  'show_ui' => true,
  'exclude_from_search' => true,
  'hierarchical' => true,
  'supports' => array( 'title', 'editor', 'thumbnail' ),
  'query_var' => true
        )
  );

Alternatively you can use register_taxonomy_for_object_type , http://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type

share|improve this answer
Please put this code in theme functions.php

function post_type_discog() {

register_post_type('discography',
    array(
    'labels' => array(
            'name' => __( 'Discography' ),
            'singular_name' => __( 'Discography' ),
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Add New Discography' ),
            'edit' => __( 'Edit' ),
            'edit_item' => __( 'Edit Discography' ),
            'new_item' => __( 'New Discography' ),
            'view' => __( 'View Discography' ),
            'view_item' => __( 'View Discography' ),
            'search_items' => __( 'Search Discographys' ),
            'not_found' => __( 'No Discographys found' ),
            'not_found_in_trash' => __( 'No Discographys found in Trash' ),
            'parent' => __( 'Parent Discography' ),
        ),
  'public' => true,
  'show_ui' => true,
        'exclude_from_search' => true,
        'hierarchical' => true,
        'supports' => array( 'title', 'editor', 'thumbnail' ),
        'query_var' => true
        )
  );
}
add_action('init', 'post_type_discog');

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

function create_discog_taxonomies()
{
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    'name' => _x( 'Recordings', 'taxonomy general name' ),
    'singular_name' => _x( 'Recording', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Recordings' ),
    'popular_items' => __( 'Popular Recordings' ),
    'all_items' => __( 'All Recordings' ),
    'parent_item' => __( 'Parent Recording' ),
    'parent_item_colon' => __( 'Parent Recording:' ),
    'edit_item' => __( 'Edit Recording' ),
    'update_item' => __( 'Update Recording' ),
    'add_new_item' => __( 'Add New Recording' ),
    'new_item_name' => __( 'New Recording Name' ),
  );
  register_taxonomy('recordings',array('discography'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'recordings' ),
  ));
}
In this code u can create custom post type and custom category type. May this code is helpful to u.
share|improve this answer
    
Thanks - but that's the bit I've already got. What I now need is to add actual categories (in this case, add actual recordings) –  George Pearce Apr 2 '12 at 14:32

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.