Skip to content

Instantly share code, notes, and snippets.

@phamngsinh
Created August 25, 2017 14:43
Show Gist options
  • Select an option

  • Save phamngsinh/f892b3c25912d57b1ada9b1db17ec830 to your computer and use it in GitHub Desktop.

Select an option

Save phamngsinh/f892b3c25912d57b1ada9b1db17ec830 to your computer and use it in GitHub Desktop.
wordpress get children of category
/**
$term_id = 1;
$taxonomy_name = 'category';
$args = array( 'parent' => $term_id);
$termchildren = get_product_children($term_id, $taxonomy_name);
**/
function get_product_children( $term_id, $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
}
$term_id = intval( $term_id );
$terms = _get_term_hierarchy($taxonomy);
if ( ! isset($terms[$term_id]) )
return array();
$children = $terms[$term_id];
foreach ($children as $key=>$child ) {
unset($children[$key]);
$children[$child] = get_term_children($child, $taxonomy);
}
return $children;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment