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
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' )
)
);
}
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 theinit
action.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).
That should read..
.. 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..
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.
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.