Skip to content

Instantly share code, notes, and snippets.

@petrusnog
Last active March 6, 2020 18:43
Show Gist options
  • Save petrusnog/d6258298ea9b01936d669ada085a30c6 to your computer and use it in GitHub Desktop.
Save petrusnog/d6258298ea9b01936d669ada085a30c6 to your computer and use it in GitHub Desktop.
PHP function that retrieves an array with all the taxonomies of certain WordPress Post Type.
<?php
//===============================================
// PHP function that retrieves an array with all
// the taxonomies of certain WordPress Post Type.
// By: SamuraiPetrus
//===============================================
// $taxonomies = get_object_taxonomies('your-post-type');
// Codex get_object_taxonomies : https://developer.wordpress.org/reference/functions/get_object_taxonomies/
function get_taxonomies_tree ($taxonomies) {
$tax_tree = [];
foreach ( $taxonomies as $tax ) {
if ( isset($tax) ){
$args = ["taxonomy" => $tax];
$tax_content = [];
$tax_terms = get_terms($args);
if (!empty($tax_terms)) {
foreach ($tax_terms as $t) {
$tax = get_taxonomy($t->taxonomy);
}
if ($tax) {
$tax_title = $tax->label;
}else{
$tax_title = "Categorias";
}
array_push($tax_content, $tax_title);
array_push($tax_content, $tax_terms);
array_push($tax_tree, $tax_content);
}
}
}
return $tax_tree;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment