If I am on a product archive page then how can I get current product category ID in product archive page. I am using this but no result:

global $wp_query;
$category_name = $wp_query->query_vars['product_cat'];
$category_object = get_term_by('name', $category_name, 'product_cat');
$category_id = $category_object->term_id;

2 Answers 2


I solve This To get the current category ID.

$cate = get_queried_object();
$cateID = $cate->term_id;
echo $cateID;

and it works like a charm.

  • it gives this error: Notice: Undefined property: WP_Post_Type::$term_id in file.php
    – sheetal
    Apr 25, 2018 at 10:35
  • @Exclutips Nice! Got the job done for me! Thank you!
    – Ruvee
    Apr 5, 2021 at 2:51

Funciona perfeitamente!

global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) {
    $product_cat_id = $term->term_id;
    if( $term = get_term_by( 'id', $product_cat_id, 'product_cat' ) ){echo $term->name;}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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