0

I have a custom post type 'events' and a taxonomy-'Event types'. How can i get all the terms that belong to custom taxonomy 'Event types' , in an array . I use wp_list_categories($args) but it gives the output with each type in ali tags.

Please help me out

code for registering taxonomy

<?php
function event_init() {
    // create a new taxonomy
    register_taxonomy(
        'Event types',
        'events',       
        array(
            'labels' => array(
                            'name'=>'Event types',
                            'add_new_item'=>'Add New Event types ',
                            'new_item_name'=>"New Event types"              
                            ),

            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true,
            'rewrite' => array( 'slug' => 'event-type' ),



             )
    );
}
add_action( 'init', 'event_init' );

?>
terminator
  • 159
  • 3
  • 19
  • "How can i get all the types of 'Event types' taxonomy, in an array". What exactly do you mean by "get all the types"? Do you mean get all of the terms that belong to the 'Event types' taxonomy? Or do you mean get all of the posts that have been tagged with a term from the 'Event types' taxonomy? You'll need to be specific. – henrywright Jul 14 '14 at 13:22
  • sorry for the confusion ,, i want all the terms that belong to the 'Event types' taxonomy – terminator Jul 14 '14 at 13:29
  • please add the code how you registered your taxonomy, as you are confusing everybody here with your taxonomy name – Pieter Goosen Jul 14 '14 at 18:49
  • i have added the code above – terminator Jul 15 '14 at 12:04

2 Answers2

2
$terms = get_terms( 'your_taxonomy_name' );

This will get you an array of term objects. Note your taxonomy name must be lowercase letters and underscores only.

Ref: http://codex.wordpress.org/Function_Reference/get_terms

henrywright
  • 10,070
  • 23
  • 89
  • 150
  • i am getting this in array object(WP_Error)#324 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid taxonomy" } } ["error_data"]=> array(0) { } } – terminator Jul 14 '14 at 13:45
  • Are you sure the name of your taxonomy is 'Event-types'? **Also note:** When you register your taxonomy you should actually make sure to use all lower case characters. See [here](http://codex.wordpress.org/Function_Reference/register_taxonomy#Parameters) – henrywright Jul 14 '14 at 13:51
  • name of the custom taxonomy is right. But is this the code to get terms objects of custom taxonomy of Custom Post type. – terminator Jul 14 '14 at 13:58
  • You're getting an error "invalid_taxonomy" - are you sure `Event-types` is the exact name of your taxonomy? – henrywright Jul 14 '14 at 14:00
  • Actually, `Event-types` cannot possibly be the name of your taxonomy because WordPress doesn't allow the `-` character in custom taxonomy names. – henrywright Jul 14 '14 at 14:04
  • yup henry i rechecked it is exactly the same name – terminator Jul 14 '14 at 14:04
  • yup i have not used '-' between Event types. – terminator Jul 14 '14 at 14:07
  • "You have not used `-` in your taxonomy name yet your taxonomy name is `Event-types`" <-- this doesn't make sense. – henrywright Jul 14 '14 at 14:40
  • no the name is Event types only and miss typed it earlier , and i used $terms = get_terms( 'Event types' ); – terminator Jul 14 '14 at 14:50
  • You can't have spaces either. – henrywright Jul 14 '14 at 15:00
  • the code ran perfectly fine this time, although i didn't do anything new. Anyways thanks a lot henry – terminator Jul 15 '14 at 23:41
1

$terms = get_terms( 'Event types' );

This will work for you. For better experience don't use capital letters and spaces in names of the taxonomy or any other variables names.

Ref:https://developer.wordpress.org/reference/functions/get_terms/

Waqas_aamer
  • 220
  • 1
  • 3
  • 18