get_terms return errors

Hi when i try to get_terms(); in theme options via this code

$catalogs_terms = get_terms( 'catalogs' );
   $mycatalogs = array( -1 => 'Select a catalog' );
   if ( $catalogs_terms ) {
      foreach ( $catalogs_terms as $catalog_term ) {
         $mycatalogs[$catalog_term->term_id] = $catalog_term->name;
      }
   }

return empty but this code is working fine every where in pages etc.
when i try to print_r( $catalogs_terms ) output i am getting errors

Read More
Array ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid Taxonomy ) ) [error_data] => Array ( ) )

i don’t understand where am i wrong?
my function for register taxonomy

    add_action( 'init', 'my_taxonomies', 0 );

function my_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name' => _x( 'Catalogs', 'taxonomy general name' ),
        'singular_name' => _x( 'Catalog', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Catalogs', 'mytextdomain' ),
        'all_items' => __( 'All Catalogs', 'mytextdomain' ),
        'parent_item' => __( 'Parent Catalog', 'mytextdomain' ),
        'parent_item_colon' => __( 'Parent Catalog:', 'mytextdomain' ),
        'edit_item' => __( 'Edit Catalog', 'mytextdomain' ), 
        'update_item' => __( 'Update Catalog', 'mytextdomain' ),
        'add_new_item' => __( 'Add New Catalog', 'mytextdomain' ),
        'new_item_name' => __( 'New Catalog Name', 'mytextdomain' ),
        'menu_name' => __( 'Catalogs', 'mytextdomain' ),
    );  

    // register catalogs hierarchical (like categories)
    register_taxonomy( 'catalogs',
        array( 'news' ),
        array( 'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'public' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'catalogs' )
        )
    );
}

Related posts

Leave a Reply

2 comments

  1. As i was mentioning before, it’s a case of your term fetching occuring before the taxonomy has been registered.

    The init action occurs after the theme’s functions file has been included, so if you’re looking for terms directly in the functions file, you’re doing so before they’ve actually been registered.

    Here’s a portion of the code from wp-settings.php that includes the theme functions and does the init action.

    // Load the functions for the active theme, for both parent and child theme if applicable.
    if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists( STYLESHEETPATH . '/functions.php' ) )
        include( STYLESHEETPATH . '/functions.php' );
    if ( file_exists( TEMPLATEPATH . '/functions.php' ) )
        include( TEMPLATEPATH . '/functions.php' );
    
    do_action( 'after_setup_theme' );
    
    // Load any template functions the theme supports.
    require_if_theme_supports( 'post-thumbnails', ABSPATH . WPINC . '/post-thumbnail-template.php' );
    
    register_shutdown_function( 'shutdown_action_hook' );
    
    // Set up current user.
    $wp->init();
    
    /**
     * Most of WP is loaded at this stage, and the user is authenticated. WP continues
     * to load on the init hook that follows (e.g. widgets), and many plugins instantiate
     * themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.).
     *
     * If you wish to plug an action once WP is loaded, use the wp_loaded hook below.
     */
    do_action( 'init' );
    

    As you can see the init action doesn’t fire until after the theme functions file is included, therefore any term retrieval must occur after init. I can’t really advise you any further though because you’ve only shown me a portion of your code, so i’ve not much of an idea of the context you’re trying to use the term function in, but it certainly can’t be called directly in the functions file(outside a callback hooked onto a specific action/filter because the code will run to soon).

    Hopefully the above illustrates the problem enough for you.. 🙂

    Additional note:
    This function is missing a var from the global statement(you’d see PHP notices if you had debug turned on).

    function news_updated_messages( $messages ) {
        global $post;
    

    That should read..

    function news_updated_messages( $messages ) {
        global $post, $post_ID;
    

    .. because code inside that function references that var, but the variable doesn’t have scope inside the function, my above suggested change will fix that.

    Follow-up #1

    When creating a plugin or theme page, you first have to setup/register that page, this is typically done like so..

    add_action('admin_menu', 'my_theme_menu');
    
    function my_theme_menu() {
        add_theme_page( 'Theme Settings', 'Theme Settings', 'manage_options', 'my-unique-identifier', 'my_theme_settings' );
    }
    function my_theme_settings() {
        
        // Code to display/handle theme options would be here
        // You get_terms() call should work inside this function just fine
    
    }
    

    If the theme page is created differently, specific to the premium theme you’re using then i can’t really help as they tend to use a framework that is entirely different to regular WordPress themes.

  2. If you don’t want to mess around with WordPress nonsense of it ignoring taxonomies which clearly exist in the database, whether they be registered or not, you can just use a replacement function like this to grab the taxonomy at any time.

    function custom_get_terms($taxonomy) {
     global $wpdb;
    
     $out = array();
    
     $a = $wpdb->get_results($wpdb->prepare("SELECT t.name,t.slug,t.term_group,x.term_taxonomy_id,x.term_id,x.taxonomy,x.description,x.parent,x.count FROM {$wpdb->prefix}term_taxonomy x LEFT JOIN {$wpdb->prefix}terms t ON (t.term_id = x.term_id) WHERE x.taxonomy=%s;",$taxonomy));
    
     foreach ($a as $b) {
      $obj = new stdClass();
      $obj->term_id = $b->term_id;
      $obj->name = $b->name;
      $obj->slug = $b->slug;
      $obj->term_group = $b->term_group;
      $obj->term_taxonomy_id = $b->term_taxonomy_id;
      $obj->taxonomy = $b->taxonomy;
      $obj->description = $b->description;
      $obj->parent = $b->parent;
      $obj->count = $b->count;
      $out[] = $obj;
     }
    
     return $out;
    }