wordpress get_terms function not working in my plugin

hello again from a far far away place.

you know i`m trying to list all of terms from a custom taxonomy , when i use below code :

Read More
$terms = get_terms($taxonomy , 'hide_empty=0');
print_r($terms);
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
  echo "<li>" . $term->name . "</li>";

}
echo "</ul>";

wp return a crazy error that says : INVALID TAXONOMY :

WP_Error Object
(
    [errors] => Array
        (
            [invalid_taxonomy] => Array
                (
                    [0] => Invalid Taxonomy
                )

        )

    [error_data] => Array
        (
        )

)

it is very interesting that you know , when i use above code in single.php, i have not see any error and it works fine.

somebody please help me !

Related posts

Leave a Reply

5 comments

  1. Resolved

    I am trying to use:

    get_terms( 'event_category', array('hide_empty'=>FALSE) );   
    

    to use my admin theme option and face same problem when using:

    add_action( 'init', 'register_features_taxonomy_event_category' );   
    

    But now its resolved using:

    add_action( 'after_setup_theme', 'register_features_taxonomy_event_category' );
    
  2. Oh my … i solve this by a crazy solution temporary.look below :

    function load_terms($taxonomy){
        global $wpdb;
        $query = 'SELECT DISTINCT 
                                    t.name 
                                FROM
                                    `wp-cls`.wp_terms t 
                                INNER JOIN 
                                    `wp-cls`.wp_term_taxonomy tax 
                                ON 
                                 `tax`.term_id = `t`.term_id
                                WHERE 
                                    ( `tax`.taxonomy = '' . $taxonomy . '')';                     
        $result =  $wpdb->get_results($query , ARRAY_A);
        return $result;                 
    } 
    

    As you can see i use a query, but i cant apply this plugin to my programming team.i still awaiting for a correct solution/usage for get_terms function in wordpress plugins.

    regards.

  3. Nothing really to add but to make it clear: get_terms()doesn’t work in “admin_init” action hook!

    I did like bizzr3. Just put my code here because I was a bit confuse with bizzr3’s code:

    function load_terms($taxonomy){
      global $wpdb;
      $query = 'SELECT DISTINCT 
                      t.name 
                  FROM
                    wp_terms t 
                  INNER JOIN 
                    wp_term_taxonomy tax 
                  ON 
                    tax.term_id = t.term_id
                  WHERE 
                      ( tax.taxonomy = '' . $taxonomy . '')';                     
      $result =  $wpdb->get_results($query , ARRAY_A);
      return $result;                 
    } 
    

    then just call load_terms() in your “admin_init” function:

    //get all terms from taxonomy Category
      $terms = load_terms('category');
    

    and thanks, works like a charm.

  4. I know I’m late to the party.
    To optimise bizzr3’s code just a bit; You should use the $wpdb object properties for your table names, as “wp_” is just the default but it could change from site to site. Using the object property you make sure it should work for other WordPress sites as well if they have a different table prefix.

    function load_terms( $taxonomy ){
        global $wpdb;
        $query = "SELECT DISTINCT 
                   t.name 
                  FROM
                   {$wpdb->terms} t 
                  INNER JOIN 
                   {$wpdb->term_taxonomy} tax 
                  ON 
                   tax.term_id = t.term_id
                  WHERE 
                   ( tax.taxonomy = '{$taxonomy}')";                     
        $result = $wpdb->get_results( $query , ARRAY_A );
    
        return $result;                 
    }
    

    Also changed the single quotes to double quotes to use {$variables}