Search code examples
wordpresswordpress-rest-api

Reason I would get 403 rest_forbidden accessing WP API taxonomies/name-of-taxonomy?


I'm trying to get a list of taxonomies using the WordPress REST API. Hitting /wp-json/wp/v2/taxonomies/post_tag works fine, but I also have a custom taxonomy called location and accessing /wp-json/wp/v2/taxonomies/location returns a 403 rest_forbidden error.

I can't figure out under what circumstances taxonomy REST access would be forbidden in this way. Any ideas?


Solution

  • You need to set show_in_rest to true when registering your taxonomy.

    https://codex.wordpress.org/Function_Reference/register_taxonomy

    If your custom taxonomy was created by a plugin and you need to alter it's behaviour try this post :

    http://scottbolinger.com/custom-taxonomies-in-the-wp-api/

    In short, you can add the code below to your functions file to enable show_in_rest for all custom taxonomies.

    function prefix_add_taxonimies_to_api() {
        $args = array(
            'public'   => true,
            '_builtin' => false
        ); 
        $taxonomies = get_taxonomies($args, 'objects');
        foreach($taxonomies as $taxonomy) {
            $taxonomy->show_in_rest = true;
        }
    }
    add_action('init', 'prefix_add_taxonimies_to_api', 30);
    

    I hope this helps you.