jeffsebring / angular-wp-api

An AngularJS Client for WP-API
124 stars 15 forks source link

Adds a resource for requesting custom post types #7

Closed pdewouters closed 10 years ago

pdewouters commented 10 years ago

Don't merge yet

The WP API doesn't follow a strict RESTful approach to URLs. For example, to get posts from a specific post type, the URL looks like this: http://local.wordpress.dev/wp-json/posts/?type=my_post_type

With the current implementation of a general resource for all requests, it's not possible to generate these URLs. Here I introduce a separate resource for getting posts and passing a parameter for the post type:

$scope.posts = WPAPI.Posts.query( {
    type: 'my_post_type'
} );

As the type parameter has no placeholder, it's converted to a query argument.

Disclaimer: I'm an AngularJS noob, so this may not be the ideal approach :) Maybe using the $http would be better as this API is not 100% RESTful?

pdewouters commented 10 years ago

Unless creating custom routes for custom post types would make it work with the default resource.

jeffsebring commented 10 years ago

I'm also an AngularJS noob, so we can help each other muddle through.

Is the result here different than if you were to use the current resource like so?

wpAPIResource.query( {
    param1: 'posts',
    type: 'post_type'
} );

I think a $resource service is perfect for most operations needed for WP-API. Is there anything it can't do for us that $http can? Perhaps we can add another service using $http if we find need?

pdewouters commented 10 years ago

Going to close this as the correct approach is to extend the WP_JSON_CustomPostType class, as in https://github.com/WP-API/WP-API/blob/master/lib/class-wp-json-pages.php