I’ve created a custom taxonomy. It’s working just as expected, aside from it is not showing in the get_taxonomies
array. get_terms
function returns an invalid taxonomy
error.
I want to use get_terms
to loop through the Double India Pale Ales and print each name for a select box.
Here is the code used to register it.
add_action( 'init', 'double_ipa_init' );
function double_ipa_init() {
register_taxonomy(
'double-ipa',
array (
0 => 'post',
1 => 'page',
),
array(
'hierarchical' => true,
'label' => 'Double IPAs',
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'double-ipa'
),
'singular_label' => 'Double IPA'
)
);
}
This code is in a plugin, and is on Multisite.
Thanks in advance for your help.
3 s
The Invalid Taxonomy
error will be raised by the function get_terms()
. You’re registring your taxonomy on the init
action hook. Therefore you have to call your get_terms()
function on the same or a later hook.
Try this snippet. It should display all term names of your taxonomy, regardless if the term is empty.
add_action('init', 'wpse29164_registerTaxonomy');
function wpse29164_registerTaxonomy() {
$args = array(
'hierarchical' => true,
'label' => 'Double IPAs',
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'double-ipa'
),
'singular_label' => 'Double IPA'
);
register_taxonomy('double-ipa', array('post', 'page'), $args);
$terms = get_terms('double-ipa', array('hide_empty' => false));
foreach ($terms as $term) {
echo $term->name;
}
}