Reputation: 1955
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 :
$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 !
Upvotes: 1
Views: 6345
Reputation: 11
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}
Upvotes: 1
Reputation: 25
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' );
Upvotes: 3
Reputation: 260
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.
Upvotes: 1
Reputation: 121
get_terms return invalid taxonomy error because: you have a custom taxonomy registered in the "init" hook so in the wp-admin it doesn´t work -> your taxonomy is registered after you call "get_term" https://wordpress.stackexchange.com/questions/13480/get-terms-return-errors/13482#13482
Upvotes: 2
Reputation: 1955
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.
Upvotes: 1