I am using the WP REST API plugin and Jetpack plugin for wordpress webservice. So, initially ‘Vehicle’ is my custom post type where it includes two types
of taxonomies i.e two-wheeler and four-wheeler. So, currently I am not getting the taxonomies in webservice result ie.
https://public-api.wordpress.com/rest/v1.1/sites/site_url/posts/?type=vehicle&status=publish&number=10&offset=0.
Following is my code.
/***************************************************
* Register a two-wheeler, four-wheeler taxonomy with Vehicle post type, with REST API support
***************************************************/
add_action( 'init', 'vehicle_group_taxonomy', 30 );
function vehicle_group_taxonomy() {
$labels = array(
'name' => _x( 'two_wheeler', 'taxonomy general name' ),
'singular_name' => _x( 'two_wheeler', 'taxonomy singular name' ),
'search_items' => __( 'Search two_wheeler' ),
'all_items' => __( 'All two_wheeler' ),
'parent_item' => __( 'Parent two_wheeler' ),
'parent_item_colon' => __( 'Parent two_wheeler:' ),
'edit_item' => __( 'Edit two_wheeler' ),
'update_item' => __( 'Update two_wheeler' ),
'add_new_item' => __( 'Add New two_wheeler' ),
'new_item_name' => __( 'New two_wheeler Name' ),
'menu_name' => __( 'two_wheeler' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'two_wheeler' ),
'show_in_rest' => true,
'rest_base' => 'two_wheeler',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'two_wheeler', array( 'vehicle' ), $args );
$labels_speciality = array(
'name' => _x( 'four_wheeler', 'taxonomy general name' ),
'singular_name' => _x( 'four_wheeler', 'taxonomy singular name' ),
'search_items' => __( 'Search four_wheeler' ),
'all_items' => __( 'All four_wheeler' ),
'parent_item' => __( 'Parent four_wheeler' ),
'parent_item_colon' => __( 'Parent four_wheeler:' ),
'edit_item' => __( 'Edit four_wheeler' ),
'update_item' => __( 'Update four_wheeler' ),
'add_new_item' => __( 'Add New four_wheeler' ),
'new_item_name' => __( 'New four_wheeler Name' ),
'menu_name' => __( 'four_wheeler' ),
);
$args_speciality = array(
'hierarchical' => true,
'labels' => $labels_speciality,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'four_wheeler' ),
'show_in_rest' => true,
'rest_base' => 'four_wheeler',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'four_wheeler', array( 'vehicle' ), $args_speciality );
}
/********************************************************
* Add REST API support to an already registered post type.
*********************************************************/
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
function my_custom_post_type_rest_support() {
global $wp_post_types;
//be sure to set this to the name of your post type!
$post_type_name = 'vehicle';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
/*******************************************************
* Add REST API support to an already registered taxonomy.
*********************************************************/
add_action( 'init', 'my_custom_taxonomy_rest_support', 25 );
function my_custom_taxonomy_rest_support() {
global $wp_taxonomies;
//be sure to set this to the name of your taxonomy!
$taxonomy_name = array('two_wheeler','four_wheeler');
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
$wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name;
$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
}
}