0

I am using custom post type : portfolio and have taxonomy for that portfolio_category. I need to get sub categories for a particular category using the above custom taxonomy.

In short, How can I get the child categories for taxonomy=portfolio_category&tag_ID=80&post_type=portfolio?

1 Answer 1

1

Use get_terms() to get the child terms of a given term. You need to feed the specific term id to either

  • parent 

(integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string.

OR

  • child_of

- (integer) Get all descendents of this term. Default is 0. Note: the difference between child_of and parent is that where parentonly gets direct children of the parent term (ie: 1 level down), child_of gets all descendants (as many levels as are available)

EXAMPLE:

Get all descendants of term ID 80

(Requires PHP 5.4+)

$terms = get_terms( 'portfolio_category', ['child_of' => 80] );

For only first level children, change child_of to parent

3
  • Thank you for the help.!But am getting only first child by this code. $terms = get_terms('portfolio_category', array("child_of" => 80)); Commented Jul 23, 2015 at 11:40
  • child_of gets all children from a given term, in this case term 80. If there is more, and they are not shown, then it means that the terms that does not show is empty, ie, they don't have posts. Commented Jul 23, 2015 at 11:50
  • @PieterGoosen is there a way to list the child even if there are no posts in existence?
    – Drewdavid
    Commented May 29, 2021 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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