0

I created a custom post type and I am trying to get the posts from it using WP-API, however, when I try to access the API I get the error rest_no_route message. The set up for my custom post type is below:

function cw_post_type_pil() {
$supports = array('title', // 
post title'editor', // post content 
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);

$labels = array(
'name' => _x('Profiles', 'plural'),
'singular_name' => _x('Profile', 'singular'),
'menu_name' => _x('Profile In Law', 'admin menu'),
'name_admin_bar' => _x('Profiles In Law', 'admin bar'),
'add_new' => _x('Add New Profile', 'add new'),
'add_new_item' => __('Add New profile'),
'new_item' => __('New Profile'),
'edit_item' => __('Edit Profile'),
'view_item' => __('View Profile'),
'all_items' => __('All Profiles'),
'search_items' => __('Search Profiles'),
'not_found' => __('No results found.'),
);

$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'capability_type'    => 'post',
'query_var' => true,
'rewrite' => array('slug' => 'pil'),
'has_archive' => true,
'hierarchical' => false,
'show_in_rest' => true,
'rest_controller_class' => 'WP_REST_Terms_Controller',
'rest_base'             => 'profiles-api',
);

register_post_type('profiles_in_law', $args);
}
add_action('init', 'cw_post_type_pil');

This is stored in my functions.php file.

Currently, I am accessing the API using the address: https://newtontest.staging.wpengine.com/wp-json/wp/v2/profiles-api

While I get the following error:

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

This post type has 38 posts in them, but no data is returned as well.

I have gone through most of the articles and questions on allowing the use of the WP API on custom post types and have been making changes to the post type definition as well, but to no avail.

I am currently running Wordpress 4.7

Any help regarding this, is much appreciated.

0

I think you have to add

'rest_controller_class' => 'WP_REST_Posts_Controller',

Instead of

'rest_controller_class' => 'WP_REST_Terms_Controller',

WP_REST_Terms_Controller is for custom taxonimies according to this documentation.

  • Glad to hear you have the solution now! – Sabbir Hasan Feb 3 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.