Prateek Goyal
Prateek Goyal

Reputation: 64

How to Add WooCommerce Product Categories in Custom Post Type

$args = array(
    'label'               => __( 'Eventr', 'dnp_theme' ),
    'description'         => __( 'What clients say ', 'dnp_theme' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'thumbnail'),
    'taxonomies'          => array( 'Eventr', 'product_cat'),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => false,
    'show_in_admin_bar'   => true,
    'menu_position'       => 10,
    'menu_icon'           => 'dashicons-images-alt2',
    'can_export'          => true,
    'has_archive'         => false,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
);

register_post_type( 'Eventr', $args );

This is My Code Using to Get Woocommerce Product Categories in my Custom Post type.

In Taxonomies i have add woocommerce product category taxonomies "product_cat" but it not to show on admin panel.

Help me to know How to add WooCommerce Product Categories in Custom Post Type Admin Menu.

Upvotes: 2

Views: 6376

Answers (3)

qsi
qsi

Reputation: 683

To anyone having this issue in 2020 and with the Gutenberg editor:

Step 1: Register the taxonomy either using register_taxonomy_for_object_type or by setting it in the post type registration arguments using the taxonomies field (as in the other answers and the question).

Step 2: Include the taxonomy in the REST API and make it available in the block editor by setting show_in_rest to true. For product_cat, this means to change this setting using the woocommerce_taxonomy_args_product_cat filter.

Upvotes: 2

Casey Milne
Casey Milne

Reputation: 39

Use the function register_taxonomy_for_object_type() called on init, but make sure the timing is right so that this call fires after your CPT registration. Notice the use of 99 as the priority setting in this case.

// attach product category taxonomy to "service" post type
add_action( 'init', function() {
    register_taxonomy_for_object_type( 'product_cat', 'service' );
}, 10, 99);

Upvotes: 1

5tormTrooper
5tormTrooper

Reputation: 923

I was having the same issue, and I found that registering the taxonomy afterwards seemed to work. So in your functions after registering the custom post type, add something like this, where custom_post_type key is the key you registered:

add_action( 'init', 'add_product_cat_to_custom_post_type' );
function add_product_cat_to_custom_post_type() {
    register_taxonomy_for_object_type( 'product_cat', 'custom_post_type' );
}

Upvotes: 7

Related Questions