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.
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 theget_term_by
function. Like so: