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!
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!
Share Improve this question edited Apr 6, 2020 at 2:02 Wei asked Apr 5, 2020 at 23:53 WeiWei 215 bronze badges 2 |2 Answers
Reset to default 2get_cat_ID
gets the ID of a category based on the name. "Category" is a specific taxonomy named category
. You're trying to get the ID of a Product Category (product_cat
). You cannot use get_cat_ID()
to get the ID of a custom taxonomy.
To do that you need to use get_term_by()
:
$product_cat = get_term_by( 'name', $cat_name, 'product_cat' );
if ( $product_cat ) {
$thisCatID = $product_cat->term_id;
}
But, based on your code, you don't need to be finding the ID. You already have it:
$cate_object = get_the_terms( $post->ID, 'product_cat' );
foreach( $cate_object as $cate ){
$thisCatID = $cate->term_id; // This is the ID.
$cat_array[] = $thisCatID;
}
For people who has the same problem: I think the problem is caused by Polylang. I checked my category list on dashboard, and realized that the category I failed to get with get_cat_ID() doesn't belong to any language (which is weird). I created a test category and give assign a language to it, then get_cat_ID() works!
Thanks to people who tried to help out!
$thisCatID
in this line ` if($thisCat !== null && $thisCat != 0)` ? – Michael Commented Apr 6, 2020 at 3:02