I need to tell whether or not the current custom taxonomy archive page I'm viewing has child categories. I've got a situation where there are a lot of custom categories with children and the site is only to show posts at the end of the line. Otherwise it should show a link to the category that's the next step down. I've found this snippet, but it doesn't seem to work for custom taxonomies.

function category_has_children() {
global $wpdb;   
$term = get_queried_object();
$category_children_check = $wpdb->get_results(" SELECT * FROM wp_term_taxonomy WHERE parent = '$term->term_id' ");
    if ($category_children_check) {
        return true;
    } else {
       return false;

    if (!category_has_children()) {
        //use whatever loop or template part here to show the posts at the end of the line
   get_template_part('loop', 'index'); 

    else {
       // show your category index page here

4 Answers 4


There may or may not be a better way to do this, but here's how I would do it:

$term = get_queried_object();

$children = get_terms( $term->taxonomy, array(
'parent'    => $term->term_id,
'hide_empty' => false
) );
// print_r($children); // uncomment to examine for debugging
if($children) { // get_terms will return false if tax does not exist or term wasn't found.
    // term has children

If current taxonomy term has children the get_terms function will return an array, otherwise it will return false.

Tested and works on my local vanilla install with Custom Post Type UI plugin used for CPT generation.

  • When I uncommented the print_r($children).... it outputted an array. How could that be turned into an if/else? Sorry, I'm still very new to php
    – user29489
    Commented Mar 28, 2013 at 17:43
  • 1
    Disregard I used this: if ($children) { echo 'Children Here'; } else { echo 'No Children'; }
    – user29489
    Commented Mar 28, 2013 at 17:52
  • @user29489 You're right, I wasn't clear enough in my answer. Edited for future reference. Commented Mar 28, 2013 at 18:02
  • 1
    For those that just need to know whether there are any children and don't need to get the data for the child terms, I suggest adding 'field' => 'count' to just count the number of children.
    – J.D.
    Commented Dec 30, 2013 at 19:54
  • Does this work with normal post categories as well?
    – Pete
    Commented Nov 19, 2018 at 0:48

There's also a generic WP possibility to do this via get_term_children.

$children = get_term_children($termId, $taxonomyName);

if( empty( $children ) ) {
    //do something here

Assuming that you are trying to filter your terms to only show terms that either have children or not, you can actually use the childless parameter in your get_terms() function.

$children = get_terms( 
    'taxonomy' => '$taxonomy_slug',
    'hide_empty' => false,
    'childless' => true

This will output an array of terms that don't have children.


This is by far the cleanest solution

$term = get_queried_object();
if($term->parent == 0 ){
  // do stuff;
  • This is incorrect. $term->parent == 0 will tell you whether or not the category is a child, but not that it has children. As an example, the stock Uncategorized category would pass your test, but it has no children.
    – Mike
    Commented Sep 11, 2021 at 20:59

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.