New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 API Backbone client - get custom post types #1299

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

Comments

Projects
None yet
5 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

This comment has been minimized.

Show comment
Hide comment
@rachelbaker

rachelbaker May 26, 2015

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 );
Member

rachelbaker commented May 26, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@joneslloyd

joneslloyd May 26, 2015

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).

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

This comment has been minimized.

Show comment
Hide comment
@rachelbaker

rachelbaker May 26, 2015

Member

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

Member

rachelbaker commented May 26, 2015

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

@joneslloyd

This comment has been minimized.

Show comment
Hide comment
@joneslloyd

joneslloyd May 26, 2015

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.

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

This comment has been minimized.

Show comment
Hide comment
@rachelbaker

rachelbaker May 26, 2015

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.

Member

rachelbaker commented May 26, 2015

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

This comment has been minimized.

Show comment
Hide comment
@joneslloyd

joneslloyd May 26, 2015

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 );
            });
        });

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 );
            });
        });
@meitar

This comment has been minimized.

Show comment
Hide comment
@meitar

meitar Apr 9, 2016

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

meitar commented Apr 9, 2016

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

@Ridder90

This comment has been minimized.

Show comment
Hide comment
@Ridder90

Ridder90 Mar 14, 2017

You shouldn't forget to add te version string like this

url: wpApiSettings.root + wpApiSettings.versionString + 'events',

You shouldn't forget to add te version string like this

url: wpApiSettings.root + wpApiSettings.versionString + 'events',

@markobojic

This comment has been minimized.

Show comment
Hide comment
@markobojic

markobojic Nov 17, 2017

@rachelbaker
I am not familiar with WP. Where should I put that peace of code, in which file?

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 );

@rachelbaker
I am not familiar with WP. Where should I put that peace of code, in which file?

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

This comment has been minimized.

Show comment
Hide comment
@joneslloyd

joneslloyd Nov 17, 2017

Hi @markobojic - I believe WP-API has changed a lot since this post I made two years ago, so you'd have to check the documentation.

WP-API is now part of WordPress core, so this type of stuff would go on the CPT declaration, rather than on an action (I believe -- Please everybody else correct me if I'm wrong 😄 )

Hi @markobojic - I believe WP-API has changed a lot since this post I made two years ago, so you'd have to check the documentation.

WP-API is now part of WordPress core, so this type of stuff would go on the CPT declaration, rather than on an action (I believe -- Please everybody else correct me if I'm wrong 😄 )

@markobojic

This comment has been minimized.

Show comment
Hide comment
@markobojic

markobojic Nov 17, 2017

I found similar solution in new Rest API documentation, but still not sure where should I add that in clients theme so I can Crete new FED application.
Tnx for comment anyway.

I found similar solution in new Rest API documentation, but still not sure where should I add that in clients theme so I can Crete new FED application.
Tnx for comment anyway.

@joneslloyd

This comment has been minimized.

Show comment
Hide comment
@joneslloyd

joneslloyd Nov 17, 2017

No worries 😄 If it's a custom theme, then you could simply add it (the CPT declaration, along with the REST stuff) into functions.php (or another PHP file which you include in functions.php), and if it's a child theme, check their guidance for adding custom code.

No worries 😄 If it's a custom theme, then you could simply add it (the CPT declaration, along with the REST stuff) into functions.php (or another PHP file which you include in functions.php), and if it's a child theme, check their guidance for adding custom code.

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