$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Why is my working Custom Taxonomy not in get_taxonomies array?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Why is my working Custom Taxonomy not in get_taxonomies array?

matteradmin8PV0评论

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.

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.

Share Improve this question asked Sep 22, 2011 at 3:31 Jeff SebringJeff Sebring 4501 gold badge4 silver badges12 bronze badges 1
  • post your code for get_taxonomies and get_terms. Also try setting public argument to true. – Assad Nazar Commented Sep 22, 2011 at 7:17
Add a comment  | 

3 Answers 3

Reset to default 14

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;
    }
}

You're looking to use get_terms() before 'Init' action hook.

Here's the order of the hooks run in a typical request:

muplugins_loaded
registered_taxonomy
registered_post_type
plugins_loaded
sanitize_comment_cookies
setup_theme
load_textdomain
after_setup_theme
auth_cookie_malformed
auth_cookie_valid
set_current_user
**init**
widgets_init
register_sidebar
wp_register_sidebar_widget
wp_default_scripts
wp_default_stypes
admin_bar_init
add_admin_bar_menus
wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
posts_selection
wp
template_redirect
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts

I got the same issue before, but using WP_Term_Query helped me retrieve what i needed give it a try it should work. More details here : https://developer.wordpress/reference/classes/WP_Term_Query/__construct/

Post a comment

comment list (0)

  1. No comments so far