get_cat_ID() not wokring


I’m writing a function in functions.php. I am working with woocommerce and trying to get posts with the same cat name of product_cat on single product pages. My idea is using the cat name of woocommerce cat and get_cat_ID() to get the category ID, then use WP_query to get the posts. However, get_cat_ID() always returns 0 (I event create a category called ‘test’, but still). Tried to google the problem but no solution. get_cat_ID() seems to be really straightforward so I am really not sure what went wrong. It would be great if anyone can let me know what would be the possible reason.

The sum up my problem: I have a product_cat named ‘test’. Now I want to get the ID of the category with the same name ‘test’.

Here’s the code:

$cate_object = get_the_terms( $post->ID, 'product_cat' );
foreach( $cate_object as $cate ){
    // get cat id by tax name
    $thisCatName = $cate->name;
    $thisCatID = get_cat_ID($thisCatName);
    if($thisCat !== null && $thisCat != 0)
        $cat_array[] = $thisCatID;


, Wei 3 years 2020-04-05T20:51:02-05:00 0 Answers 122 views 0

Leave an answer