WP-API / node-wpapi

An isomorphic JavaScript client for the WordPress REST API
http://wp-api.org/node-wpapi/
MIT License
1.68k stars 190 forks source link

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

Closed manuelpoelzl closed 4 years ago

manuelpoelzl commented 4 years ago

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?

CyberCyclone commented 4 years ago

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);
kadamwhite commented 4 years ago

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

kadamwhite commented 4 years ago

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