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.

2 Answers 2

1

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.

1
  • Glad to hear you have the solution now! Feb 3, 2019 at 15:50
1

Another thing helped me. Please make sure that the new post type is not defined only for the administrator but for all roles.

class Custom_classes_Loader {

    /**
     * Constructor
     *
     * @since 1.0.0
     * @return void
     */
    public function __construct() {

        spl_autoload_register( array( $this, 'autoload' ) );

        //If I use libraries, I have to include them here.

        $this->includes();

        if ( is_admin() ) {
            $this->includes_admin();
        } else {
            $this->includes_frontend();
        }

    }
        /**
     * Includes classes available everywhere
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function includes() {
        // this is available to all roles and now works 
        require_once SPRINTERLMS_PLUGIN_DIR . 'includes//class.plugin.post-types-create.php';
    }

    /**
     * Includes classes available only on administration panel
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function includes_admin() {
        //
    }

     /**
     * Includes classes available only on public
     *
     * @since 1.0.0
     *
     * @return void
     */
    public function includes_public() {
        //
    }
}
2
  • I don't recognise includes_admin and includes_public. Is this part of a specific plugin framework, in a class definition?
    – Rup
    May 20, 2021 at 10:58
  • This is kind of a mental shortcut. I didn't think it might be unreadable. Thank you for your attention, I will correct the content of my answers. The code you see is a simple derivative of the is_admin () condition. ` $this->includes(); if ( is_admin() ) { $this->includes_admin(); } else { $this->includes_frontend(); } ` May 21, 2021 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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