I am having issues retrieving a custom category name. I already have its ID, which is coming to me from a custom field. I don't know if it makes any difference, but I'm using Woocommerce.

I'm trying to do this:

<?php get_term_by( 'id', $my_custom_cat_id, 'product-cat') ?>

But I get an empty value back, even though I know the category ID is correct (I am using it as query args already)

Many thanks

  • That code should work. Please file an edit with how you retrieve the value from you custom field and how are you using the result from get_term_by – Pieter Goosen Nov 19 '14 at 14:55

See the inline comments. Not tested. The following code will grab all the Custom Taxonomy Terms of Custom Taxonomy 'product-cat' and will show them one by one from the result array.

global $post;
$postID = $post->ID //get/put your post ID here
$getProductCat = get_the_terms( $postID, 'product-cat' ); //as it's returning an array
foreach ( $getProductCat as $productInfo ) {
    echo $productInfo->name;
  • I'd love to say this work, but as soon as $postID = $post->ID is into the page, the page stops rendering – alemur Nov 19 '14 at 10:15
  • How are you grabbing post_id? I just put that globally. Grab the post id from your context and pass there. I said there get or put - it your turn. I don't know where you are implementing th code. :) – Mayeenul Islam Nov 19 '14 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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