3

I am using the REST API to create posts.

I am able to create normal posts, but I would like to create posts of custom type.

http://example.com/wp-json/wp/v2/posts

I am trying to POST to the above URL with data as

title: 'Loreum Ipsum',
content: 'Test Post',
post_type: custom_type

which doesn't create the post of type custom_type instead normal post is created.


I tried posting to

http://example.com/wp-json/wp/v2/posts?post_type=custom_post

with the data,

title: 'Loreum Ipsum',
content: 'Test Post',

but still it creates normal post.


I also have tried to send the data as

title: 'Loreum Ipsum',
content: 'Test Post',
type: custom_type

to http://example.com/wp-json/wp/v2/posts

which also creates normal post.


I am using POSTMAN to send the data. What else should I try?

Any help or suggestions appreciated!

  • 1
    post_type is not a valid argument when creating a post. See the documentation. To use custom post types with the REST API you need to enable support for it, which will create a dedicated endpoint for it. Again, see the documentation. – Jacob Peattie Feb 14 '18 at 13:42
  • 1
    does your post type specify that it should be exposed in REST when you register it? – Tom J Nowell Feb 14 '18 at 14:22
5

Make sure your post type is shown in the REST API.

$args = array(
  //* Use whatever other args you want
  'show_in_rest'          => true,
  'rest_base'             => 'myslug',
  'rest_controller_class' => 'WP_REST_Posts_Controller',
);
register_post_type( 'myslug', $args );

The endpoint to create a post would then be http://example.com/wp-json/wp/v2/myslug.

Edit:

The above is all that's needed for a custom post type to be available as a REST endpoint using the default WP_REST_Posts_Controller. I initially had the following code, because I think it makes using the REST API easier. However, as pointed out in the comments, it's not needed to answer this question. You can just use the endpoint.

function wpse294085_wp_enqueue_scripts() {
  wp_enqueue_script( 'wp-api' );
  wp_enqueue_script( 'my-script', PATH_TO . 'my-script.js', [ 'wp-api' ] );
}
add_action( 'wp_enqueue_scripts', 'wpse294085_wp_enqueue_scripts' );

Then in my-script.js, just use Backbone.

wp.api.loadPromise.done( function() {
  var post = new wp.api.models.Myslug( {
    'id': null,
    'title': 'Example New Post',
    'content': 'YOLO'
  } );
  var xhr = post.save();
});
  • The point of having a restful API is to not to depend on some specific technology to sue it, otherwise it is not an API,and only the JS interface is. – Mark Kaplun Feb 14 '18 at 16:49
  • 1
    ... answer is probably much more correct if you remove the JS related stuff – Mark Kaplun Feb 14 '18 at 16:52
  • Huh? Backbone isn't required to use the REST API. It's just really easy, so why not. The endpoint is still there. – Nathan Johnson Feb 14 '18 at 16:53
  • it is not easy at all to use it from outside of a browser. – Mark Kaplun Feb 14 '18 at 17:32
  • 1
    Is the rest_controller_class even necessary? I've never needed it myself, only show_in_rest – Tom J Nowell Feb 15 '18 at 0:20

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.