Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active June 23, 2017 19:23
Show Gist options
  • Save scottopolis/b23417448e079a765f1f to your computer and use it in GitHub Desktop.
Save scottopolis/b23417448e079a765f1f to your computer and use it in GitHub Desktop.
Add multiple custom post types to the WP-API
<?php
// This is a temporary solution until register_post_type_args is available
// After adding your CPT, you can find it at mysite.com/wp-json/wp/v2/[slug]
function sb_add_cpts_to_api() {
global $wp_post_types;
// Add CPT slugs here
$arr = ['fake','movie','books'];
foreach( $arr as $key ) {
// If the post type doesn't exist, skip it
if( !$wp_post_types[$key] )
continue;
$wp_post_types[$key]->show_in_rest = true;
$wp_post_types[$key]->rest_base = $key;
}
}
add_action( 'init', 'sb_add_cpts_to_api', 30 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment