WP Snipp Ninja

Get current category (taxonomy) children and prepare them for options element

Author: Dimitar Radev | Posted on: August 13, 2018 |

Today I will share something that I use often. Getting the children of the current queried category or taxonomy and making from them an array containing slugs and titles (names).

This PHP function is very helpful when creating a <select> dropdown for subcategories filter for example.

* Get current category (taxonomy) children and prepare them for options element. 
* @return array

function get_product_cat_children()
  if (is_tax('product_category')) {
    $termID = get_queried_object()->term_id;
    $term_children = get_term_children( $termID, 'product_category' );
    $children_arr = array();
    foreach ($term_children as $child) {
      $term = get_term_by('id', $child, 'product_category');
      $child_val = $term->slug;
      $child_name =$term->name;
      $children_arr[] = array('val' => $child_val, 'name' => $child_name );

    return $children_arr;

Usage example


Article keywords

  • About the Author

     Dimitar Radev

    Hey, I'm Dimitar "B1-0S" Radev or D in short. I'm a WordPress Developer for nearly 10 years and I have in-depth knowledge in creating Custom WordPress Themes, Plugins, Commercial Themes Edits, and WooCommerce Storefronts. In the last two years, I'm specializing in Technical SEO for WordPress websites.

    Follow Dimitar Radev on Social Media

    Leave a Reply

    Your email address will not be published. Required fields are marked *