6

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 );

}
?>
  • 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
9

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' );
}
  • 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 – Nicolai Nov 25 '13 at 20:59
  • That's strange ! it is not working for me – user1760110 Nov 25 '13 at 21:01
  • 2
    +1 on register_taxonomy_for_object_type( 'item', 'product' ); – Magico Jun 5 '14 at 14:17
  • 1
    Worked perfectly after adding both lines register_taxonomy( 'item', 'product', $args ); register_taxonomy_for_object_type( 'item', 'product' ); – Ariful Haque Oct 26 '16 at 10:42

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