You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Adding Endpoints](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/): create custom REST API endpoints for your plugin or application
5
6
6
7
[Working with Custom Content Types](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/): learn how to interact with your [custom post types](https://developer.wordpress.org/plugins/post-types/) and [custom taxonomies](https://developer.wordpress.org/plugins/post-types/) through the REST API
7
8
8
9
[Modifying responses](https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/): add fields to REST API response objects using `register_rest_field`
10
+
11
+
9
12
## Resources
13
+
10
14
[Defining your API Schema](https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/): define the schema for your REST API resources and their arguments
11
15
12
16
[Glossary](https://developer.wordpress.org/rest-api/extending-the-rest-api/glossary/): get up to speed with phrases used throughout our documentation
13
17
14
-
[Routes & Endpoints](https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/): dive deeper into the nuances of REST API routes and the endpoints they provide
18
+
[Routes & Endpoints](https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/): dive deeper into the nuances of REST API routes and the endpoints they provide
15
19
16
20
[Controller Classes](https://developer.wordpress.org/rest-api/extending-the-rest-api/controller-classes/): discover how to structure and extend REST API endpoint controller classes
# Adding REST API Support For Custom Content Types
2
2
3
3
The REST API can create routes for custom post types and custom taxonomies inside of the `wp/v2` namespace, using the same controllers as the default post type or taxonomy term controllers. Alternatively, you can use your own controllers and namespace. This document will cover using the default controllers for your custom content type's API routes. This is the easiest way and ensures the highest chance of compatibility with third parties.
4
+
5
+
4
6
## Registering A Custom Post Type With REST API Support
5
-
When registering a custom post type, if you want it to be available via the REST API you should set `'show_in_rest' => true` in the arguments passed to `register_post_type`. Setting this argument to true will add a route in the `wp/v2` namespace.
7
+
8
+
When registering a custom post type, if you want it to be available via the REST API you should set `'show_in_rest' => true` in the arguments passed to `register_post_type`. Setting this argument to true will add a route in the `wp/v2` namespace.
6
9
7
10
```php
8
11
/**
@@ -13,9 +16,9 @@ When registering a custom post type, if you want it to be available via the REST
13
16
add_action( 'init', 'my_book_cpt' );
14
17
function my_book_cpt() {
15
18
$args = array(
16
-
'public' => true,
17
-
'show_in_rest' => true,
18
-
'label' => 'Books'
19
+
'public' => true,
20
+
'show_in_rest' => true,
21
+
'label' => 'Books'
19
22
);
20
23
register_post_type( 'book', $args );
21
24
}
@@ -36,47 +39,49 @@ Here is an example of registering a post type, with full labels, support for the
36
39
add_action( 'init', 'my_book_cpt' );
37
40
function my_book_cpt() {
38
41
$labels = array(
39
-
'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
40
-
'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
## Registering A Custom Taxonomy With REST API Support
79
-
Registering a custom taxonomy with REST API support is very similar to registering a custom post type: pass `'show_in_rest' => true` in the arguments passed to `register_taxonomy`. You may optionally pass `rest_base` to change the base url for the taxonomy's routes.
83
+
84
+
Registering a custom taxonomy with REST API support is very similar to registering a custom post type: pass `'show_in_rest' => true` in the arguments passed to `register_taxonomy`. You may optionally pass `rest_base` to change the base url for the taxonomy's routes.
80
85
81
86
The default controller for taxonomies is `WP_REST_Terms_Controller`. You may modify this with the `rest_controller_class` if you choose to use a custom controller.
## Adding REST API Support To Existing Content Types
132
+
126
133
When a custom post type or custom taxonomy has been added by code that you do not control, for example a theme or plugin you are using, you may need to add REST API support after it has alredy been registered. The arguments are the same as in the previous examples, but need to be added to the global `$wp_post_types` and `$wp_taxonomies` arrays.
127
134
128
135
Here is an example of adding REST API support to an existing custom post type:
@@ -138,10 +145,10 @@ function my_custom_post_type_rest_support() {
138
145
//be sure to set this to the name of your post type!
If you are having trouble implementing either of these examples, be sure that you are adding these hooks with a sufficiently high priority. If the callback functions run before the post type or taxonomy is registered, then the `isset` check will prevent an error, but the support will not be added.
180
+
181
+
173
182
## Custom Link Relationships
174
-
Taxonomies & custom post types have a built-in association within WordPress, but what if you want to establish a link between two custom post types? This is not supported formally within WordPress itself, but we can create our own connections between arbitrary content types using the `_link` relation.
183
+
184
+
Taxonomies & custom post types have a built-in association within WordPress, but what if you want to establish a link between two custom post types? This is not supported formally within WordPress itself, but we can create our own connections between arbitrary content types using the `_link` relation.
0 commit comments