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've just converted all of my blog posts to a CPT because I needed them to be hierarchical. Problem is, I had categories assigned to all of the posts. How can I use the standard WordPress categories in my CPT?

I've tried adding 'taxonomies' => array('category'), to the register_post_type function, but that didn't work. And I tried register_taxonomy_for_object_type( 'category', 'illustrations' ) and that didn't work either.

Any suggestions?

UPDATE: Thanks all for the replies but as mentioned above — I've tried both 'taxonomies' => array('category') and 'taxonomies' => array('category'), and neither have worked.

Here's the whole register_post_type function — with the 'taxonomies' parameter:

/* Register the 'Illustrations' post type */

register_post_type(
    'illustrations',
    array(
        'label' => 'illustrations',
        'description' => '',
        'public' => true,
        'menu_icon' => get_bloginfo( 'template_directory' ) . '/images/help-icon.png',
        'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'post',
        'taxonomies' => array( 'category' ),
        'has_archive' => true,
        'hierarchical' => true,
        'rewrite' => array( 'slug' => '' ),
        'query_var' => true,
        'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'page-attributes', 'revisions', 'comments','author' ),
        'labels' => array (
            'name' => 'Illustrations',
            'singular_name' => 'Illustration',
            'menu_name' => 'Illustrations',
            'add_new' => 'Add Illustration',
            'add_new_item' => 'Add new Illustration',
            'edit' => 'Edit',
            'edit_item' => 'Edit illustration',
            'new_item' => 'New illustration',
            'view' => 'View illustration',
            'view_item' => 'View illustration',
            'search_items' => 'Search illustrations',
            'not_found' => 'No Illustrations Found',
            'not_found_in_trash' => 'No Illustrations Found in Trash',
            'parent' => 'Parent Illustration',
            ), ) );

register_taxonomy_for_object_type( 'category', 'illustrations' );
share|improve this question

4 Answers 4

up vote 2 down vote accepted

The answer lies in register_taxonomy_for_object_type. Very simple usage.

You could also try passing the 'taxonomies' argument in your register_post_type.

share|improve this answer
    
Thanks for the reply — but this doesn't seem to work for me. –  saltcod Sep 19 '13 at 17:45
    
You're doing the registration on the init hook, correct? –  Eric Holmes Sep 19 '13 at 18:05
    
ACK! That got deleted and I never noticed. Thanks so much for the help — once I had that part in there, everything worked out A-ok. –  saltcod Sep 20 '13 at 16:49

By default the queries for category pages will only query the post post type, so in addition to registering the post type with category in the taxonomies argument, you have to modify those queries via pre_get_posts to get the CPT to appear on category pages:

function wpa_illustrations_on_category_pages( $query ) {
    if ( $query->is_category() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'illustrations' ) );
    }
}
add_action( 'pre_get_posts', 'wpa_illustrations_on_category_pages' );
share|improve this answer

Maybe try this:

    /** Add Custom Taxonomy **/
    function illustrations_category_taxonomy() {
        $labels = array(
            'name'              => __( 'Categories' ),
            'singular_name'     => __( 'Category' ),
            'search_items'      => __( 'Search Categories' ),
            'all_items'         => __( 'All Categories' ),
            'parent_item'       => __( 'Parent Category' ),
            'parent_item_colon' => __( 'Parent Category:' ),
            'edit_item'         => __( 'Edit Category' ), 
            'update_item'       => __( 'Update Category' ),
            'add_new_item'      => __( 'Add New Category' ),
            'new_item_name'     => __( 'New Category' ),
            'menu_name'         => __( 'Categories' ),
        );
        $args = array(
            'labels'            => $labels,
            'hierarchical'      => true
        );
        register_taxonomy( 'illustrations-category', 'illustrations', $args );
    }
    add_action( 'init', 'illustrations_category_taxonomy');
share|improve this answer

You need to use the taxonomies argument like this:

    'taxonomies' => array('category')

In your register_post_type code.

Additional information: http://codex.wordpress.org/Function_Reference/register_post_type#Arguments


Update:

/* Register the 'Illustrations' post type */

    add_action( 'init', 'wpse114838_reg_pt_illustrations');
    function wpse114838_reg_pt_illustrations() { 
        register_post_type(
            'illustrations',
            array(
                'labels' => array (
                'name' => 'Illustrations',
                'singular_name' => 'Illustration',
                'menu_name' => 'Illustrations',
                'add_new' => 'Add Illustration',
                'add_new_item' => 'Add new Illustration',
                'edit' => 'Edit',
                'edit_item' => 'Edit illustration',
                'new_item' => 'New illustration',
                'view' => 'View illustration',
                'view_item' => 'View illustration',
                'search_items' => 'Search illustrations',
                'not_found' => 'No Illustrations Found',
                'not_found_in_trash' => 'No Illustrations Found in Trash',
                'parent' => 'Parent Illustration',
                ),
                'label' => 'illustrations',
                'description' => '',
                'public' => true,
                'menu_icon' => get_bloginfo( 'template_directory' ) . '/images/help-icon.png',
                'show_ui' => true,
                'show_in_menu' => true,
                'capability_type' => 'post',
                //you need just one of the methods, uncomment the one you want
                //'taxonomies' => array( 'category' ),
                'has_archive' => true,
                'hierarchical' => true,
                'rewrite' => array( 'slug' => '' ),
                'query_var' => true,
                'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'page-attributes', 'revisions', 'comments','author' ),
            )
        );
        //you need just one of the methods, uncomment the one you want    
        //register_taxonomy_for_object_type( 'category', 'illustrations' );
    }

This is based on the code in your question. I tested it, with both - register_taxonomy_for_object_type( 'category', 'illustrations' ); or 'taxonomies' => array( 'category' ) - methods, and it's working. Just use one of them, there is no sense in doing both, uncomment the one you want in the code above. You probably didn't hook into the init action, like @EricHolmes and @Howdy_McGee suggested.

share|improve this answer
    
Thanks for the reply — but this doesn't seem to work for me. Any ideas why? –  saltcod Sep 19 '13 at 17:45
    
Just to follow up on this, the revised answer is certainly working. –  ialocin Nov 7 '13 at 13:08

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.