Resources on WordPress json_no_route when querying taxonomy with WP-API

json_no_route when querying taxonomy with WP-API

To get a taxonomy, using WP-API, you should make a request to http://example.com/wp-json/taxonomies/category/

All is well, until you have a taxonomy with a dash(‘-‘) in its name – in my case, the taxonomy is called ‘event-categories’ – the default event taxonomy in the Events Manager plugin.

So, a request to http://example.com/wp-json/taxonomies/event-categories/ responds with a “No route was found matching the URL and request method” message.

It’s a pretty big, apparently known issue, and will hopefully be resolved soon. In case it’s not though, here is a temporary solution, stolen from GitHub user zkingdesign on this page:

function ONetWPAPIDashFix ( $endpoints ) {
    $new_endpoints = array();
    foreach ( $endpoints AS $match => $data ) {
        $new_endpoints[ str_replace( 'w+', '(?:[w-](?<!_))+', $match ) ] = $data;
    }
    return $new_endpoints;
}
add_filter('json_endpoints','ONetWPAPIDashFix',10,1);

 


Comments

Leave a Reply

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