I want to GET data from my custom post type using WP REST API.
My Custom post type is "result" and I have tried with this parameters.
http://ejobbox.com/wp-json/wp/v2/result/?
http://ejobbox.com/wp-json/wp/v2/result/?per_page=10
and also with
http://ejobbox.com/wp-json/wp/v2/posts/?post_type=result
But I am not able to get data using custom Post type.
I have also added this into my custom post type
'show_in_rest'=> true,
'rest_base' => 'result',
'rest_controller_class' => 'WP_REST_Posts_Controller',
Still I didn't get any result.
Please, can you tell me what I am doing wrong here and what can I do to get data from my custom post type? Please Give me a suggestion for this.
My Function.php (Custom post type) code is Here:
function codexres_custom_init() {
$args = array(
'public' => true,
'label' => 'Result'
);
register_post_type( 'result', $args );
}
add_action( 'init', 'codexres_custom_init' );
function codex_result_init() {
$labels = array(
'name' => _x( 'Result', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Result', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Result', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Result', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'Result', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Result', 'your-plugin-textdomain' ),
'new_item' => __( 'New Result', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Result', 'your-plugin-textdomain' ),
'view_item' => __( 'View Result', 'your-plugin-textdomain' ),
'all_items' => __( 'All Result', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Result', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Result:', 'your-plugin-textdomain' ),
'not_found' => __( 'No Result found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No Result found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'query_var' => true,
'menu_icon' => 'dashicons-admin-users',
'rewrite' => array( 'slug' => __('result', 'result')),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'rest_base' => 'result',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies' => array('result','category', 'post_tag')
);
register_post_type( 'result', $args );
}
What you did wrong:
You have two function codexres_custom_init
and codex_result_init
And both function registering same post type which is not required. Although for second function codex_result_init
you did not add it to add_action('init','function_name')
. So in your case the function codexres_custom_init
is not required. For further understaning on custom post type creation can see this document
Try this , what i can see is that, you register result post type twice. although you did not init the main one.
function codex_result_init() {
$labels = array(
'name' => _x( 'Result', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Result', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Result', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Result', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'Result', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Result', 'your-plugin-textdomain' ),
'new_item' => __( 'New Result', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Result', 'your-plugin-textdomain' ),
'view_item' => __( 'View Result', 'your-plugin-textdomain' ),
'all_items' => __( 'All Result', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Result', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Result:', 'your-plugin-textdomain' ),
'not_found' => __( 'No Result found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No Result found in Trash.', 'your-plugin-textdomain' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'your-plugin-textdomain' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'query_var' => true,
'menu_icon' => 'dashicons-admin-users',
'rewrite' => array( 'slug' => __('result', 'result')),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'rest_base' => 'result',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
'taxonomies' => array('result','category', 'post_tag')
);
register_post_type( 'result', $args );
}
add_action( 'init', 'codex_result_init' );
Just eliminate below code:
function codexres_custom_init() {
$args = array(
'public' => true,
'label' => 'Result'
);
register_post_type( 'result', $args );
}
add_action( 'init', 'codexres_custom_init' );
Dont inculde this ya.