Inside a Post / Custom Post type loop, I am trying to get child terms selected on the edit screen of that post/custom post.

For Example:

  1. Zoologist ( Parent )

    • Pre Zoologist
    • Post Zoologist

Now if for any specific post / custom post if users selected all of above taxonomies including Zoologist (Parent), Pre Zoologist, Post Zoologist.

Using get_the_terms function i only want to get child taxomony, in this case Pre Zoologist, Post Zoologist.

I tried this code but it is not working:

$terms = get_the_terms( get_the_ID(), 'custom_taxonomy' );

  foreach ( $terms as $term ){
  if ( $term->parent == 0 ) {
    echo '<div class="parent '.$term->slug.'">' . $term->name . '</div>';


Please let me know where am I making a mistake.

  • 1
    Your code is only displaying terms without carents. you want the Opposite. Change == to !==. – Jacob Peattie Jul 20 '19 at 10:25
  • 1
    @JacobPeattie Thanks for guiding me. Can you please post it as an answer so that others can get help. – Nimesh Jul 20 '19 at 17:15

Your Answer

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

Browse other questions tagged or ask your own question.