Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have an odd issue with get_term_by(). It's working fine when I use it inside a template, but when I use it in functions.php it just returns false.

$term = get_term_by('slug', 'some-term', 'some-taxonomy');
var_dump($term);

Any explanation as to why this is happening would be greatly appreciated.

share|improve this question
    
Can you show the actual code? There's a chance that there is something wrong with the values sent to the function. –  tollmanz Aug 28 '11 at 14:23
    
This is the actual code, I've change the term names to something generic, however in my test I would just copy-paste the line from functions.php to header.php and it would just work. –  Andrei Aug 28 '11 at 15:01
add comment

1 Answer

up vote 1 down vote accepted

This is probably happening because the taxonomy you're trying to query is registered yet. Eg. The WordPress environment is loaded when a theme's functions.php file loads, but many plugins/themes/core functions don't register taxonomies until later.

Try hooking into init with a really high priority number and running the get_term_by function. Like so:

<?php
add_action( 'init', 'wpse27111_tester', 999 );
function wpse27111_tester()
{
    $term = get_term_by('slug', 'some-term', 'some-taxonomy');
    var_dump($term);
}
share|improve this answer
    
Works great! I was suspecting it was something of that nature, thanks for the tip! –  Andrei Sep 7 '11 at 7:05
    
TY so much! Just spent an hour trying to figure out why post categories was working and my custom taxonomies weren't... hooked to init and all is better. –  jb510 Aug 9 '13 at 4:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.