get_term_by not working when in functions.php

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 Comment