Last active
June 23, 2017 19:23
-
-
Save scottopolis/b23417448e079a765f1f to your computer and use it in GitHub Desktop.
Add multiple custom post types to the WP-API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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