Skip to content

Instantly share code, notes, and snippets.

@enricodeleo
Last active December 1, 2022 16:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save enricodeleo/7157359 to your computer and use it in GitHub Desktop.
Save enricodeleo/7157359 to your computer and use it in GitHub Desktop.
Get and list all the taxonomies attached to a custom post type in Wordpress
<?php
$taxonomy_objects = get_object_taxonomies( 'cpt', 'objects' ); // <--- change cpt with the custom post type
$out = array();
foreach ( $taxonomy_objects as $taxonomy_slug => $taxonomy ){
$terms = get_terms( $taxonomy_slug, 'hide_empty=0' );
if ( !empty( $terms ) ) {
$out[] = "<strong>" . $taxonomy->label . "</strong>\n<ul>";
foreach ( $terms as $term ) {
$out[] =
" <li>"
. $term->name
. " </li>\n";
}
$out[] = "</ul>\n";
}
}
echo implode('', $out );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment