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.
To 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;
}
Thanks!
$thisCatID
in this line ` if($thisCat !== null && $thisCat != 0)` ?