If you want to retrieve all of the terms that are within all of the registered taxonomies for a specific post type you can use a function like the one below.
function termsByTaxonomiesPostType( $postType ) {
if ( ! post_type_exists( $postType ) ) {
return [];
}
$list = [];
$taxonomies = get_object_taxonomies( $postType );
if ( empty( $taxonomies ) ) {
return [];
}
foreach ( $taxonomies as $taxonomy ) {
$terms = get_terms( [
'taxonomy' => $taxonomy,
'fields' => 'all',
] );
// Not an array if you pass 'count' as value for 'fields'.
if ( ! is_array( $terms ) || is_wp_error( $terms ) ) {
continue;
}
$list[ $taxonomy ] = [];
// Note: if you want 'id=>slug', 'id=>name', 'names', 'id=>parent', 'ids', 'slugs' only
// You dont need to perform this.
foreach ( $terms as $term ) {
$list[ $taxonomy ] = array_merge(
$list[ $taxonomy ],
[
$term->name => $term->slug,
]
);
}
}
return $list;
}
You're list will contain a list of taxonomies and for each taxonomy a list of key => value pairs where the key is the name
of the term and the value the slug
.
To note that if you want to retrieve 'id=>slug', 'id=>name', 'names', 'id=>parent', 'ids', 'slugs' only you don't need to perform an additional loop because WordPress will do it for you.
To retrieve one of the associations listed above, just set the string as value for the key fields
.
get_terms
, then loop through the array of term objects and create your array