Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 8ctopotamus/a383b5e9e41f6cc3a028761274f84150 to your computer and use it in GitHub Desktop.
Save 8ctopotamus/a383b5e9e41f6cc3a028761274f84150 to your computer and use it in GitHub Desktop.
After adding the properties (indicated by comment: /* Required to expose to WP-JSON API */ ) to your register_post_type() function, you should be able to get your CPT data as JSON by visiting example.com/wp-json/wp/v2/cpt-endpoint.
//Register the post type
register_post_type('podcasts', array(
'label' => 'Podcasts',
'description' => 'A library of podcasts.',
'public' => true,
'show_in_rest' => true, /* Required to expose to WP-JSON API */
'rest_base' => 'podcasts', /* Required to expose to WP-JSON API */
'rest_controller_class' => 'WP_REST_Posts_Controller', /* Required to expose to WP-JSON API */
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'podcasts', 'with_front' => true),
'query_var' => true,
'menu_icon' => 'dashicons-controls-volumeon',
'supports' => array('title','editor','excerpt','thumbnail','post-formats'),
'labels' => array (
'name' => 'Podcasts',
'singular_name' => 'podcast',
'menu_name' => 'Podcasts',
'add_new' => 'Add Podcast',
'add_new_item' => 'Add New podcast',
'edit' => 'Edit',
'edit_item' => 'Edit Podcast',
'new_item' => 'New Podcast',
'view' => 'View podcast',
'view_item' => 'View Podcast',
'search_items' => 'Search Podcasts',
'not_found' => 'No Podcasts Found',
'not_found_in_trash' => 'No Podcasts Found in Trash',
'parent' => 'Parent podcast',
)
)
);
// Register the taxonomy
register_taxonomy( 'podcast_categories',
array(0 => 'podcasts'),
array(
'show_in_rest' => true, /* Required to expose to WP-JSON API */
'rest_base'=> 'podcast-categories', /* Required to expose to WP-JSON API */
'rest_controller_class' => 'WP_REST_Terms_Controller', /* Required to expose to WP-JSON API */
'hierarchical' => true,
'label' => 'Podcast Categories',
'show_ui' => true,
'query_var' => true,
'show_admin_column' => false,
'labels' => array (
'search_items' => 'Podcast Category',
'popular_items' => '',
'all_items' => 'View All',
'parent_item' => 'Choose Parent',
'parent_item_colon' => '',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'add_new_item' => 'Add New',
'new_item_name' => 'New Item Name',
'separate_items_with_commas' => '',
'add_or_remove_items' => 'Add or Remove',
'choose_from_most_used' => '',
)
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment