9

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 );
}
3
  • Seems like it is working right now. I am getting data if i visit this url ejobbox.com/wp-json/wp/v2/posts/?post_type=result Commented Jan 31, 2018 at 7:30
  • It is wrong data that get form my default post type: please check this (default post) and compare it with custom post so you understand this. it will same data. ejobbox.com/wp-json/wp/v2/posts?per_page=10 Commented Jan 31, 2018 at 7:33
  • 1
    Hello Friends can you solve this problem ? Commented Jan 31, 2018 at 7:42

5 Answers 5

12

This is how I get data back from my custom post type in WordPress using REST API.

http://ejobbox.com/wp-json/wp/v2/customposttype This will return the first 10 results of the custom post type entries.

Sign up to request clarification or add additional context in comments.

Comments

7

If anyone is using Custom Post type UI plugin - you'll need to go into the post type setting and do this through the UI.

enter image description here

5 Comments

what's the url for this new custom post type?
I'm not sure why the docs are THE MOST CONFUSING THING EVER (for me...) developer.wordpress.org/rest-api/using-the-rest-api I'll double-check... but here's a starting point.
perpetual.education/wp-json/acf/v3/person (for example) - this is one of our (unfinished) custom post types. (and you probably need to add acf to json plugin.
thanks, now I'm using another plugin wordpress.org/plugins/…
That seems like a great option!
4

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

Rest api for custom post type

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.

Comments

1

use this

//add this to your functions.php file in your theme folder
function sb_add_cpts_to_api( $args, $post_type ) {
if ( 'result' === $post_type ) {
$args['show_in_rest'] = true;
}
return $args;
}
add_filter( 'register_post_type_args', 'sb_add_cpts_to_api', 10, 2 );

i got that from here

it worked like voodo

Comments

0

try to use this 'show_in_rest' => true, in $args

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.