Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the ID from a product (1345) how can i get the category name of that specific product?

I try

$post_categories = wp_get_post_categories( $post->ID );
print_r( $post_categories );

but it outputs:

Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( )

What those it means ?

Thanks

share|improve this question
2  
What does the ID represent? What is a 'product'? Is this a custom post type? A meta field? What? –  s_ha_dum Nov 23 '12 at 15:37
    
Hi, It's a woocommerce product and it's a post type. –  Rodrigo Sanz Nov 23 '12 at 19:35

2 Answers 2

up vote 4 down vote accepted

Since the question is tagged woocommerce, i'm assuming that it's a product CPT created by woocommerce wordpress plugin. This answer doesn't apply if that's not the case.

The products categories is not normal categories, they are a custom taxonomy created specifically for products which is just labeled as "Categories".

You should go through the woocommerce documentation to find some function that would do this for you, if you don't find anything you can try an alternative solution. For that, first you should know the name of the taxonomy. You can copy it from inside the url in your browser when you visit categories edit screen in the backend. Then you can use wp_get_post_terms to get the terms.

share|improve this answer
    
Hi, thanks for your answer. And yes is a woocommerce product in wordpress. Ok, i'm going to try it, what are the $args for wp_get_post_terms ? i see the "taxonomy" and "args" are optional, so i'm going to try only with the ID. –  Rodrigo Sanz Nov 23 '12 at 19:37
    
It won't work only with ID. The default taxonomy is post_tag. You need to pass the name of the taxonomy there. It will work without $args but you can use it if you want. It's meant to override the defaults as explained on this page –  Mridul Aggarwal Nov 24 '12 at 13:17
    
I tested but it doesn't output the categiry that the product is in. I used this, <?php $term_list = wp_get_post_terms(1345, 'product_tag', array("fields" => "all")); print_r($term_list); ?> –  Rodrigo Sanz Nov 25 '12 at 16:03
    
it outputs this :Array ( [0] => stdClass Object ( [term_id] => 104 [name] => new [slug] => new [term_group] => 0 [term_taxonomy_id] => 104 [taxonomy] => product_tag [description] => Hola qué tal esto es una descripción? [parent] => 0 [count] => 8 ) ) Array ( [0] => stdClass Object ( [term_id] => 104 [name] => new [slug] => new [term_group] => 0 [term_taxonomy_id] => 104 [taxonomy] => product_tag [description] => Hola qué tal esto es una descripción? [parent] => 0 [count] => 8 ) ) –  Rodrigo Sanz Nov 25 '12 at 16:05
2  
+1, basically this was it already. –  Johannes Pille Nov 27 '12 at 19:35

I answered my own question, this work for me :

<?php 
$term_list = wp_get_post_terms($id_product,'product_cat',array('fields'=>'ids'));
$cat_id = (int)$term_list[0];
echo get_term_link ($cat_id, 'product_cat');

?>

Thanks Mridul Aggarwal for your help

share|improve this answer
2  
You could at least upvote Mridul's answer, if you don't think it's complete enough to be accepted as correct. Clearly it got you on the right track. –  Johannes Pille Nov 27 '12 at 19:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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