I have registered a new post type as:

$this->sconfig= ['post_type'=> 'event', 'slug'=>'events'];
add_action('init', array($this, 'register_event_posttype'));


function register_event_posttype()
        $args= [
            'labels' => [ 'name' => __('Events'), 'singular_name' => __('Event')],
            'supports' => ['title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'comments', 'revisions', 'post-formats'],
            'query_var' => true,
            'show_ui' => true,
            'show_in_menu'=> true,
            'show_in_nav_menus'=> true,
            'exclude_from_search'=> false,
            'capability_type' => 'post',
            'public' => true,
            'publicly_queryable' => true,
            'has_archive' => true,
            'hierarchical' => false,
            'rewrite' => array('slug' => $this->sconfig['slug']),
            'taxonomies' => array('category', 'post_tag'),
            'rest_base' => $this->sconfig['slug'],
            'show_in_rest' => true,
            //'rest_controller_class' => 'WP_REST_Posts_Controller'
        register_post_type($this->sconfig['post_type'], $args);

Now this url (http://localhost/install05/wp-json/wp/v2/events/114?_locale=user) gives a 404 status with output (triggered when trying to save draft custom post):

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

enter image description here

Note: I have some custom meta fields associated with this post type and they are appearing fine in the add/edit page.

What I tried so far (without results):

#1: Unregistering and registering the custom post type and flushing the rewrite rules (re-saving the permalink structure)

#2: Changing to this (in case of conflict): $this->sconfig= ['post_type'=> 'my-event', 'slug'=>'my-events'];


Defining the custom route:

function rest_route_for_events($route, $post) {
        if($post->post_type === $this->sconfig['post_type'] ) {
            $route = '/wp/v2/'.$this->sconfig['slug'].'/' . $post->ID;
        return $route;
add_filter('rest_route_for_post', array($this, 'rest_route_for_events'), 10, 2);

Thanks for reading this far and some more for giving some thoughts over it :).

  • "triggered when trying to save draft custom post" - is it your event post, and how did you save the post? Did you do it via JS, PHP, or was it done via the block-based post editor when you edit the post? If you did it programmatically, then what is your code and how/when/where did you run it?
    – Sally CJ
    Commented Feb 16, 2023 at 2:52
  • I was trying to save from wordpress admin (block editor).
    – sariDon
    Commented Feb 16, 2023 at 3:09
  • 1
    I tried your code and it worked fine for me - I didn't get the error in question when saving a draft event post via the block editor. So try deactivating plugins and/or your other custom code, and see if the same issue persists. But, how did you check for that error, and did the editor show that or any error? Maybe you can share one or two screenshots (showing the error)?
    – Sally CJ
    Commented Feb 16, 2023 at 3:49
  • Here is a screenshot: i.imgur.com/yYSQAbI.jpg
    – sariDon
    Commented Feb 16, 2023 at 5:04
  • 1
    Finally got it. Posted as answer. So many thanks for your intervention.
    – sariDon
    Commented Feb 16, 2023 at 12:11

1 Answer 1


The registration code was called inside the is_admin(). Moving the code outside solved the issue.

$this->sconfig= ['post_type'=> 'event', 'slug'=>'events'];
add_action('init', array($this, 'register_event_posttype'));
  • It never crossed my mind you'd do that admin check 😅, so +1 😉 But, did you do that intentionally, and if so, why?
    – Sally CJ
    Commented Feb 16, 2023 at 12:56
  • 1
    Unless I give the information in the question, it is impossible for anyone to figure that out 😅. I did it on premonition to save execution time by not reloading the setup codes every time the application loads.
    – sariDon
    Commented Feb 16, 2023 at 13:26

Your Answer

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

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