You need to pass an additional argument to get_terms()
. The default is to hide “empty” terms– terms which are assigned to no posts.
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false,
]);
EDIT:
Incase you want to display the name or the slug of the enlisted custom taxonomies being held by the $terms variable you can use this piece of code bellow:
foreach ($terms as $term){
echo $term->slug." : ";
echo $term->name;
echo "<br><br>";
}
Where $term->slug
outputs the slug of the taxonomy item that’s enlisted and $term->name
outputs the name of the according taxonomy item.