Search code examples
phpwordpressadvanced-custom-fieldscustom-taxonomy

How to solve "Invalid Taxonomy", displays Array array on WordPress page?


When I var_dump($term_list);

$term_list = wp_get_post_terms($post->ID, 'functiongroups', array("fields" => "names")); 

foreach ($term_list as $term) { $i++;
         echo '.$term.';
         echo ($i > 0 && !($i % 2 == 0)) ? ', ' : '';
};

I'm getting this:

WP_Error Object ([errors] => Array ([invalid_taxonomy] => Array ([0]
=> Invalid Taxonomy)) [error_data] => Array ())

, and displays Array array on WordPress page and not the expected taxonomy.


Solution

  • The name of the taxonomy didn't exist (i.e. a taxonomy created using a plugin like advanced-custom-fields or any other plugin using taxonomies) or was misspelled when using wp_get_post_terms();. (In my case 'functiongroups' didn't exist);

    Note that the taxonomy could be added programmatically as well, not only by using a plugin:

    function functiongroups_init() {
        // create a new taxonomy
        register_taxonomy(
            'functiongroups',
            'post',
            array(
                'label' => __( 'Functiongroups' ),
                ...
            )
        );
    }
    add_action( 'init', 'functiongroups_init' );