V2 API Backbone client - get custom post types #1299

Closed
joneslloyd opened this Issue May 26, 2015 · 7 comments

Projects

None yet

3 participants

@joneslloyd

Hi there,

I'm trying to get custom post types via the javascript client in the V2 API.

When I try this code:

self.events = new wp.api.collections.Posts;

        self.events.fetch({
            data: {
                type: 'event',
                context: 'view',
                filter: {
                   nopaging: true
                }
            }
        }).done( function() {
            self.events.each( function( an_event ) {
                console.log( an_event.attributes );
            });
        });

Nothing happens.

However if I change it to (for testing purposes) use (say) Pages:

self.events = new wp.api.collections.Pages;

        self.events.fetch().done( function() {
            self.events.each( function( an_event ) {
                console.log( an_event.attributes );
            });
        });

It works correctly.

Do you know how I can retrieve custom post types via the WP-API V2 javascript api? (Or if it's possible).

Thanks!

@rachelbaker
Member

@joneslloyd custom post types are only included if they have the attribute show_in_rest = true. Here is an example of how you would allow the events custom post type to be included in the API routes:

function wpsd_add_events_args() {
    global $wp_post_types;

    $wp_post_types['events']->show_in_rest = true;
    $wp_post_types['events']->rest_base = 'events';
    $wp_post_types['events']->rest_controller_class = 'WP_REST_Posts_Controller';
}
add_action( 'init', 'wpsd_add_events_args', 30 );
@joneslloyd

Hello - thanks for getting back to me.

I can confirm that I've already got the above in my plugin code (and proof of this working is that if I visit https://mysite.com/wp-json/wp/v2/event/6269/ I get the JSON custom post type object returned in my browser).

@rachelbaker
Member

@joneslloyd It doesn't look like the JS API currently supports v2 custom post type routes.

@joneslloyd

Oh no!

Do you know if there's a way I can force this to work? Custom routes, etc.?

I'd be happy to contribute even, to get them included.

@rachelbaker
Member

You can extend wp.api.models.Post so you can change the urlRoot to WP_API_Settings.root + '/events
See: https://github.com/WP-API/WP-API/blob/develop/wp-api.js#L356

In v2.0 each custom post type is its own route, so the type parameter doesn't work.

@joneslloyd

Fantastic! Works a treat - cheers.

Working code for anybody Googling:

var MyEvent = wp.api.models.Post.extend({
            urlRoot: WP_API_Settings.root + '/event',
            defaults: {
                type: 'event'
            }
        });

        var MyEvents = wp.api.collections.Posts.extend({
            url: WP_API_Settings.root + '/events',
            model: MyEvent
        });

        self.events = new MyEvents;

        self.events.fetch({
            filter: {
               nopaging: true
            }
        }).done( function() {
            self.events.each( function( myevent ) {
                console.log( myevent.attributes );
            });
        });
@joneslloyd joneslloyd closed this May 26, 2015
@meitar
meitar commented Apr 9, 2016

Just a note that WP_API_Settings as shown above is no longer correct. It's now wpApiSettings. :\

@arturgithub arturgithub referenced this issue in WP-API/node-wpapi Dec 26, 2016
Closed

How to fetch posts of custom post type? #283

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment