Expose Post Types and Taxonomies in the REST API
To be able to use a custom post type or taxonomy in a relation input, it must be exposed in the REST API.
TIP
You can use the post and term input types instead. These do not require to expose your post types or taxonomies in the REST API.
If your custom post types and taxonomies are not yet visible over the WordPress REST API (e.g. example.com/wp-json/wp/v2/[restBase]), you can expose it using helper functions like the ones below (which are already defined in the Corporate Template):
Post Type Helper
/**
* Exposes a post type on the REST API, so it can be accessed by a WPCE relation input.
*/
function expose_post_type(string $post_type): void
{
if (!is_user_logged_in()) return;
if (post_type_exists($post_type)) {
// Post type is already registered when this function is invoked
$args = (array) get_post_type_object($post_type);
$args['show_in_rest'] = true;
register_post_type($post_type, $args);
} else {
// Post type is not yet registered when this function is invoked therefore add filter
add_filter('register_post_type_args', function ($args, $current_post_type) use ($post_type) {
if ($current_post_type === $post_type) {
$args['show_in_rest'] = true;
}
return $args;
}, 10, 2);
}
}Taxonomy Helper
/**
* Exposes a taxonomy on the REST API, so it can be accessed by an WPCE relation input.
* Requires WPCE > v2.5.6.
*/
function expose_taxonomy(string $taxonomy): void
{
if (!is_user_logged_in()) return;
if (taxonomy_exists($taxonomy)) {
// Taxonomy is already registered when this function is invoked
$args = (array) get_taxonomy($taxonomy);
$related_post_types = $args['object_type'];
$args['show_in_rest'] = true;
register_taxonomy($taxonomy, $related_post_types, $args);
} else {
// Taxonomy is not yet registered when this function is invoked therefore add filter
add_filter('register_taxonomy_args', function($args, $current_taxonomy, $object_type) use ($taxonomy) {
if ($current_taxonomy === $taxonomy) {
$args['show_in_rest'] = true;
}
return $args;
}, 10, 3);
}
}Copy & Paste Code Snippets
The helper functions above manage the timing for us. We do not have to mind when our code is called or when the post type or taxonomy is registered. Thats a good thing that we dont have to manage this ourselves (less error prone).
The following snippets show examples if you do not want to use the helper functions.
Expose Already Registered Post Type
// Expose post type in REST when post type is already registered.
if (is_user_logged_in()) {
$post_type = 'wpcf7_contact_form';
$args = (array) get_post_type_object($post_type);
$args['show_in_rest'] = true;
register_post_type($post_type, $args);
}Expose Not yet Registered Post Type
// Expose post types in REST when post types are not yet registered.
add_filter('register_post_type_args', function ($args, $post_type) {
$post_types = ['slider', 'wpcf7_contact_form', 'employee']; // expose these custom post types in REST API
if (in_array($post_type, $post_types)) {
$args['show_in_rest'] = true;
}
return $args;
}, 10, 2);Expose Not Yet Registered Taxonomy
// Expose taxonomies if they are not yet registered.
// Taxonomies are possible since WPCE v2.5.6
add_filter('register_taxonomy_args', function($args, $taxonomy, $object_type) {
$taxonomies = ['employee-category']; // expose these custom taxonomies in REST API
if ( in_array( $taxonomy, $taxonomies ) ) {
$args['show_in_rest'] = true;
}
return $args;
}, 10, 3);