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 am trying to add a Custom Taxonomy to Woocommerce by targeting the product post type in Woocommerce. I used following code and added it into functions.php I am not getting any error message but the taxonomy is not showing in the Woocommerce as well. Can you please let me know how I can do this or what I am doing wrong here?

<?php
// Register Custom Taxonomy
function custom_taxonomy_Item()  {

$labels = array(
    'name'                       => 'Items',
    'singular_name'              => 'Item',
    'menu_name'                  => 'Item',
    'all_items'                  => 'All Items',
    'parent_item'                => 'Parent Item',
    'parent_item_colon'          => 'Parent Item:',
    'new_item_name'              => 'New Item Name',
    'add_new_item'               => 'Add New Item',
    'edit_item'                  => 'Edit Item',
    'update_item'                => 'Update Item',
    'separate_items_with_commas' => 'Separate Item with commas',
    'search_items'               => 'Search Items',
    'add_or_remove_items'        => 'Add or remove Items',
    'choose_from_most_used'      => 'Choose from the most used Items',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'item', 'product', $args );
}
?>

Update

<?php
add_action( 'init', 'custom_taxonomy_Item' );

// Register Custom Taxonomy
function custom_taxonomy_Item()  {

$labels = array(
    'name'                       => 'Items',
    'singular_name'              => 'Item',
    'menu_name'                  => 'Item',
    'all_items'                  => 'All Items',
    'parent_item'                => 'Parent Item',
    'parent_item_colon'          => 'Parent Item:',
    'new_item_name'              => 'New Item Name',
    'add_new_item'               => 'Add New Item',
    'edit_item'                  => 'Edit Item',
    'update_item'                => 'Update Item',
    'separate_items_with_commas' => 'Separate Item with commas',
    'search_items'               => 'Search Items',
    'add_or_remove_items'        => 'Add or remove Items',
    'choose_from_most_used'      => 'Choose from the most used Items',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy_for_object_type( 'item', 'product', $args );

}
?>
share|improve this question
    
what action have you hooked your custom_taxonomy_Item function to? –  Milo Nov 25 '13 at 20:39
    
Honestly, I didn't know what action to hook? I couldnt find which part of plugin in handling this? –  user1760110 Nov 25 '13 at 20:46
add comment

1 Answer 1

up vote 1 down vote accepted

You have to do:

add_action( 'init', 'custom_taxonomy_Item' );

Because:

Use the init action to call this function. Calling it outside of an action can lead to troubles.

see codex page register_taxonomy. Besides that :

Better be safe than sorry when registering custom taxonomies for custom post types. Use register_taxonomy_for_object_type() right after the function to interconnect them. Else you could run into minetraps where the post type isn't attached inside filter callback that run during parse_request or pre_get_posts.

So better add:

register_taxonomy_for_object_type( 'item', 'product' );

Additionally to reading the linked codex pages you could take a look at:

All this together should get you started.



Edit:

Like I said in the comment, it's working for me, this is the Code:

add_action( 'init', 'custom_taxonomy_Item' );
function custom_taxonomy_Item()  {
$labels = array(
    'name'                       => 'Items',
    'singular_name'              => 'Item',
    'menu_name'                  => 'Item',
    'all_items'                  => 'All Items',
    'parent_item'                => 'Parent Item',
    'parent_item_colon'          => 'Parent Item:',
    'new_item_name'              => 'New Item Name',
    'add_new_item'               => 'Add New Item',
    'edit_item'                  => 'Edit Item',
    'update_item'                => 'Update Item',
    'separate_items_with_commas' => 'Separate Item with commas',
    'search_items'               => 'Search Items',
    'add_or_remove_items'        => 'Add or remove Items',
    'choose_from_most_used'      => 'Choose from the most used Items',
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'item', 'product', $args );
register_taxonomy_for_object_type( 'item', 'product' );
}
share|improve this answer
    
Hi ialocin, thanks for your comment and hints.I updated the code code as I posted on the top but "Item" still not showing in Woocomerce Menu –  user1760110 Nov 25 '13 at 20:53
    
You have to add register_taxonomy_for_object_type and not replace register_taxonomy. It's working on my development environment. BTW product attributes are custom taxonomies too. @user1760110 –  ialocin Nov 25 '13 at 20:59
    
That's strange ! it is not working for me –  user1760110 Nov 25 '13 at 21:01
    
thanks it works now BUT, it is not adding any Term to the product. if you click on the Item and try to add new Item it is not doing any thing! –  user1760110 Nov 25 '13 at 21:09
    
Can't confirm this, it' working fine for me. @user1760110 –  ialocin Nov 25 '13 at 21:13
show 4 more comments

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.