These are chat archives for WP-API/node-wpapi

8th
Mar 2017
ajv
@andreasvirkus
Mar 08 2017 08:08
@sunpietro I can help you out in 10. In a meeting atm
ajv
@andreasvirkus
Mar 08 2017 08:13
import WPAPI from 'wpapi';

const WPAPI = new WPAPI({
  endpoint: 'your-endpoint/wp-json'
});
WP.player = WP.registerRoute('wp/v2', '/sp_player/(?P<id>)');

// You can now query it with
WP.player();
(wp/v2 is your default REST namespace in Wordpress, if it differs, change it)
sunpietro
@sunpietro
Mar 08 2017 09:52
I'll test it, thanks
sunpietro
@sunpietro
Mar 08 2017 15:38
@andreasvirkus I did as you suggested and I'm getting the rest_no_route error
what can cause it?
ajv
@andreasvirkus
Mar 08 2017 15:41
Your custom post type has to also be exposed on Wordpress side
sunpietro
@sunpietro
Mar 08 2017 15:42
I'm not sure what you mean. How can I expose the post type?
sunpietro
@sunpietro
Mar 08 2017 15:48
I think I got it:
function expose_rest_post_types() {
    global $wp_post_types;

    $wp_post_types['sp_player']->show_in_rest = true;
    $wp_post_types['sp_player']->rest_base = 'sp_player';
    $wp_post_types['sp_player']->rest_controller_class = 'WP_REST_Posts_Controller';
}
add_action('init', 'expose_rest_post_types', 30);
added it to functions.php
@andreasvirkus thanks a lot for suggestions :)
ajv
@andreasvirkus
Mar 08 2017 16:05
Sorry, I got dragged away from my desk. Glad you figured it out!
Usually show_in_rest is set when you register a post type
K Adam White
@kadamwhite
Mar 08 2017 17:33
Apologies for having been unavailable, thanks for helping each other out!