Back to all posts

How to get data of custom post type in WordPress REST API?


If you access data in REST API by default you will get all data from “posts”.

But if you created a custom post type such as “Projects” you will not able to get data through REST API. To activate it make sure ‘show_in_rest’ argument is set to true, which enables REST API support for your post type.

You can do this by adding the following argument when you register your post type:

'show_in_rest' => true

Complete code.

In functions.php

//Project Post type
    register_post_type('projects', array(
      'show_in_rest' => true,
      'rewrite' => array('slug' =>'projects'),
      'has_archive' => true,
      'public' => true,
      'supports' => array('title','thumbnail','editor','page-attributes','excerpt'),
     
      'labels' => array(
        'name' => 'Projects',
        'add_new_item' => 'Add New Project',
        'edit_item' => 'Edit Project',
        'all_items' => 'All Projects',
        'singular_name' => 'Project'
      ),
    ));