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.

1
1

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);
}

Leave a Reply

Your email address will not be published. Required fields are marked *