Skip to content

Instantly share code, notes, and snippets.

@enricodeleo
Last active July 9, 2024 13:52
Show Gist options
  • 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