-
Notifications
You must be signed in to change notification settings - Fork 191
Support for retrieving custom post types. #158
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
Conversation
Restores the ability to use `wp.post().type('my_post_type').get()`.
Credit to @jasonphillip's implementation, this version fixes the
associated tests as well as making it compatible with the most recent
master.
jasonphillips@c3b1854
|
Thanks for this, @elyobo (and @jasonphillips). One of the other WIP PR's here will expose custom resources as top-level chain initiators (so |
|
Works for me, I'm not wedded to this approach, I just wanted a way to access the CPT data and this implementation is consistent with the docs in README.md. The approach in #144 also looks good (and could possibly be used to reimplement posts/pages/media as well). |
|
@kadamwhite Do you have any clearer idea of where you're going to go custom post types in here or #144? |
This supersedes both #144 and #158 by providing a method that developers can use to arbitrarily support any endpoint. --------------------------------------- Support for Custom Post Types is provided via the `.registerRoute` method. This method returns a handler function which can be assigned to your site instance as a method, and takes the [same namespace and route string arguments as `rest_register_route`](http://v2.wp-api.org/extending/adding/#bare-basics): ```js var site = new WP({ endpoint: 'http://www.yoursite.com/wp-json' }); site.myCustomResource = site.registerRoute( 'myplugin/v1', '/author/(?P<id>)' ); site.myCustomResource().id( 17 ); // => myplugin/v1/author/17 ``` The string `(?P<id>)` indicates that a level of the route for this resource is a dynamic property named ID. By default, properties identified in this fashion will not have any inherent validation. This is designed to give developers the flexibility to pass in anything, with the caveat that only valid IDs will be accepted on the WordPress end. You might notice that in the example from the official WP-API documentation, a pattern is specified with a different format: this is a [regular expression](http://www.regular-expressions.info/tutorial.html) designed to validate the values that may be used for this capture group. ```js var site = new WP({ endpoint: 'http://www.yoursite.com/wp-json' }); site.myCustomResource = site.registerRoute( 'myplugin/v1', '/author/(?P<id>\\d+)' ); site.myCustomResource().id( 7 ); // => myplugin/v1/author/7 site.myCustomResource().id( 'foo' ); // => Error: Invalid path component: foo does not match (?P<a>\d+) ``` Adding the regular expression pattern (as a string) enabled validation for this component. In this case, the `\\d+` will cause only _numeric_ values to be accepted. **NOTE THE DOUBLE-SLASHES** in the route definition here, however: `'/author/(?P<id>\\d+)'` This is a JavaScript string, where `\` _must_ be written as `\\` to be parsed properly. A single backslash will break the route's validation. Each named group in the route will be converted into a named setter method on the route handler, as in `.id()` in the example above: that name is taken from the `<id>` in the route string. The route string `'pages/(?P<parentPage>[\d]+)/revisions/(?P<id>[\d]+)'` would create the setters `.parentPage()` and `id()`, permitting any permutation of the provided URL to be created. To permit custom parameter support methods on custom endpoints, a configuration object may be passed to the `registerRoute` method with a `mixins` property defining any functions to add: ```js site.handler = site.registerRoute( 'myplugin/v1', 'collection/(?P<id>)', { mixins: { myParam: function( val ) { return this.param( 'my_param', val ); } } }); ``` This permits a developer to extend an endpoint with arbitrary parameters in the same manner as is done for the automatically-generated built-in route handlers. Auto-discovery of all available routes will be supported in the near future, as will re-utilizing existing mixins (like `.search()`) on custom routes.
|
Provisionally superseded by #176, input requested on that PR! |
|
#176 has merged |
|
Cool, thanks @kadamwhite; been busy so haven't been able to play with the changes yet but they looked good! |
|
@elyobo thanks! If you find any issues with the interface we've implemented please let us know :) |
Restores the ability to use
wp.post().type('my_post_type').get().Credit to @jasonphillip's implementation, this version fixes the
associated tests as well as making it compatible with the most recent
master.
jasonphillips@c3b1854