1

I'm having some troubles with CPTs defined with custom rest_namespace at register_post_type. I keep getting an 404 error at the console. The error shows that the namespace is not being changed in autosave route.

WP default namespace: wp/v2

Custom namespace defined: ek/v1

I dug into the core classes to find how Wordpress handle custom namespace.

The WP_REST_Autosaves_Controler define the namespace using get_post_type_object at the __construct method:

...
$post_type_object = get_post_type_object( $parent_post_type );
....
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
...

If I do a discovery request at the ek/v1 I can see the endpoints registered correctly:

"/ek/v1/releases/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)"

But in the console this error pops up:

GET http://localhost:8888/wp-json/wp/v2/releases/70/autosaves?context=edit&_locale=user 404 (Not Found)

So, why the default namespace persist if I did all right? Would it be some javascript function of wordpress that would be doing the request on the incorrect route? What function? How to fix it?

enter image description here

1
  • can you add some context for why you're trying to change the wp/v2 namespace to ek/v1? As well as the PHP code you've used to do this?
    – Tom J Nowell
    Commented Jun 3, 2022 at 15:32

1 Answer 1

1

So, why the default namespace persist if I did all right? Would it be some javascript function of wordpress that would be doing the request on the incorrect route? What function? How to fix it?

If the endpoint works then you changed it correctly, but if you've flushed rewrite rules/permalinks, and it still isn't working, then you need to raise a bug with the Gutenberg github, or the official WP Trac.

Note that this is assuming you're only trying to change the REST API endpoint of a single custom post type, and doing it via register_post_type and the rest_namespace parameter with WP 5.9/6 or higher.

Otherwise, it looks based on this code that you have found a WordPress/Gutenberg bug with retrieving autosaves:

https://github.com/WordPress/gutenberg/blob/c0df23894233e851f3fe734358831ad560a18448/packages/core-data/src/resolvers.js#L332

5
  • At the time of writing there is some awareness of this parameters in Gutenberg github.com/WordPress/gutenberg/blob/… and has been for at least 7 months now, if you're not on 6.0 I would upgrade immediately
    – Tom J Nowell
    Commented Jun 3, 2022 at 16:00
  • 2
    I've created a PR to try and fix this, totally untested though github.com/WordPress/gutenberg/pull/41542 and you should still create a ticket on the Gutenberg GitHub as quickly as possible. It's very likely my fix will be rejected for not having a ticket
    – Tom J Nowell
    Commented Jun 3, 2022 at 16:09
  • I'm using 6.1-alpha version, and I did flush rewrite. How can I test your PR? I'll open a ticket in trac. Thanks!
    – hgodinho
    Commented Jun 4, 2022 at 0:55
  • core.trac.wordpress.org/ticket/55915#ticket
    – hgodinho
    Commented Jun 4, 2022 at 1:07
  • I'll mark your answer as correct, since you've created a PR to correct it
    – hgodinho
    Commented Jun 6, 2022 at 13:52

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.