WordPress function get_the_terms() returns ‘Invalid taxonomy’ error

It seems that the function get_the_terms( $id, $taxonomy ) always returns an error if used outside of WordPress (i.e. integrated in PHP using this method). I believe some library might be required and should be included explicitly for this to work.

For example, the following code, where 284 is a valid post id and ‘keyword’ is a valid custom taxonomy…

Read More
$terms = get_the_terms( 284, 'keyword' );
var_dump($terms);

… works perfect when used in the admin UI (via plugin), but the same does give an ‘Invalid taxonomy’ error when used outside admin UI using this method

Any suggestions?

Additional info:

The taxonomy ‘keyword’ is registered at the hook ‘init’

Taxonomy registration code:

add_action( 'init', array( $this, 'create_taxonomy' ) );
public function create_taxonomy() {
    $keyword_args = array(
    'labels' => array(
        'name'              => 'Keywords',
        'singular_name'     => 'Keyword',
        'search_items'      => 'Search Keywords',
        'all_items'         => 'All Keywords',
        'edit_item'         => 'Edit Keyword',
        'update_item'       => 'Update Keyword',
        'add_new_item'      => 'Add New Keyword',
        'new_item_name'     => 'New Keyword Name',
        'popular_items' => 'Popular Acresses',
        'not_found'     => 'No Keywords found',
        'menu_name'         => 'Keywords',
    ),
    'hierarchical'  => false,
    'public'  => true
    );   
    register_taxonomy('keyword', array('post'), $keyword_args);
}

Code outside WordPress that uses get_the_terms():

define('WP_USE_THEMES', false);
require('../../../../wp-blog-header.php' );

$terms = get_the_terms( 284, 'keyword' );
var_dump($terms);

Related posts