Tektriks

Coding starts here
Home / Blog

How to get WordPress Categories and Subcategories as a multi-dimentional array

WordPress has improved much more after integrating REST Api in their CMS. This gives us much more flexibility for building mobile applications. Though currently it is providing some basic functions but you can extend its functionality in any directions.

If you are working with web services where your back-end is running on WordPress, then you might have needed to serve wordpress categories and subcategories as parent-child or multi-dimentional array where child array are pushed under their parent. Now, I am sharing this code which will help you in future. We can fetch taxonomy hierarchy by any single taxonomy or multiple taxonomies.


<?php

/**
* Recursively get taxonomy and its children
*
* @param string $taxonomy
* @param int $parent - parent term id
* @return array
*/
function get_taxonomy_hierarchy( $taxonomy, $parent=0) {
	// only 1 taxonomy
	$taxonomy=is_array( $taxonomy) ? array_shift( $taxonomy): $taxonomy;
	// get all direct decendants of the $parent
	$terms=get_terms( $taxonomy, array( 'parent'=> $parent, 'hide_empty'=> 0));
	// prepare a new array.  these are the children of $parent
	// we'll ultimately copy all the $terms into this new array, but only after they
	// find their own children
	$children=array();
	// go through all the direct decendants of $parent, and gather their children
	foreach ( $terms as $term) {
		// recurse to get the direct decendants of "this" term
		$term->children=get_taxonomy_hierarchy( $taxonomy, $term->term_id);
		// add the term to our new array
		$children[ $term->term_id]=$term;
	}
	// send the results back to the caller
	return $children;
}




/**
 * Recursively get all taxonomies as complete hierarchies
 *
 * @param $taxonomies array of taxonomy slugs
 * @param $parent int - Starting parent term id
 *
 * @return array
 */
function get_taxonomy_hierarchy_multiple( $taxonomies, $parent = 0 ) {
	if ( ! is_array( $taxonomies )  ) {
		$taxonomies = array( $taxonomies );
	}
	$results = array();
	foreach( $taxonomies as $taxonomy ){
		$terms = get_taxonomy_hierarchy( $taxonomy, $parent );
		if ( $terms ) {
			$results[ $taxonomy ] = $terms;
		}
	}
	return $results;
}
// Example below
$hierarchies = get_taxonomy_hierarchy_multiple( array( 'category', 'post_tag' ) );

?>

Leave a Reply

Be the First to Comment!

Notify of
avatar