Using WP_Query for categories instead of get_terms

by user7432810   Last Updated May 05, 2018 14:08 PM

I have managed to get all parent categories from Woocommerce with this function

$terms = get_terms( array(
    'taxonomy' => 'product_cat',
    'hide_empty' => false,
    'parent'    => 0
) );

But I haven't managed to do the same with WP_Query. Now I have 2 questions:

  1. How can I do the same - getting a list of categories from Woocommerce - with WQ_Query
  2. Is it recommended to use WP_Query over get_terms?

Thanks.



Answers 1


$category_detail=get_the_category(get_the_ID());//$post->ID
foreach($category_detail as $cd)
{
    $name_cat=$cd->cat_name;
    $id_cat=$cd->term_id;
}

try now this code

nikhil gadhiya
nikhil gadhiya
May 05, 2018 14:10 PM

Related Questions


Filter Terms by FacetWP

Updated February 03, 2018 20:08 PM

Get post by find term value

Updated August 15, 2017 16:08 PM

How to make WP_Term_Query the Main Query?

Updated February 03, 2018 20:08 PM

Get terms from current WP Query

Updated May 07, 2017 01:08 AM