WP-API/node-wpapi

Creating post with custom post type always defaults to 'post'

manuelpoelzl opened this issue · 3 comments

Hello everybody!

We are trying to create a new post with a custom post type. We have installed the Basic Auth Plugin and we are authenticated without any problems. But now, when we try to create a post, the type of the generated post is always 'post'. The custom post has been made available in the rest API and is also shown in the result of the wpapi.types() function. Now the question, how can I create a post with a custom PostType?

Here's a rough idea of how to do it assuming your theme is setup to expose the custom post type (some do it by default)

var wp = new WPAPI({
    endpoint: settings.wpEndpoint,
    username: settings.wpUsername,
    password: settings.wpPassword  
});

wp.you_post_type = wp.registerRoute('wp/v2', 'you_post_type');

var postResponse = await wp.you_post_type().create(postObj);

@CyberCyclone's example here looks correct! Here's the relevant page in the documentation site: http://wp-api.org/node-wpapi/custom-routes/

Closing as the "how to" question appears addressed by the comments. Please add a comment below if that is incorrect. :)