How to get subcategory of category in WordPress

June 20, 2018

When you develop WordPress plug-in widget etc you will have to get subcategory of a given category. You can use the following code to get the subcategory

We are going to use get_terms($taxonomies, $args) function. If you want to learn more about this function please have look at the code reference

$parent_category_id = 4 // category id of parent

$taxonomies = array( 
    'category',
);

$args = array(
    'parent'         => $parent_category_id,
    // 'child_of'      => $parent_category_id, 
); 

$terms = get_terms($taxonomies, $args);