Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get data from custom post type using WP REST API

Tags:

rest

wordpress

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 );
}
like image 863
ejobbox Avatar asked May 09 '26 22:05

ejobbox


2 Answers

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.

like image 64
Karthik N G Avatar answered May 11 '26 14:05

Karthik N G


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

like image 42
sheriffderek Avatar answered May 11 '26 16:05

sheriffderek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!