How to Add REST API Support to WordPress Custom Post Types

In this tutorials, we are going to add WP REST API support to custom post types in our WordPress application.

Adding REST API support while creating custom post type:

While registering a custom post type, you want to make it compatible with REST API, you need add few additional arguments to the register_post_type function. The three arguments you need to pass are as follows;

  1. show_in_rest (Default: False) – Setting this argument to true will make your post type available to REST API.
  2. rest_base (Default: Post type’s name) – The base slug of the URL which will be used to access via REST API.
  3. rest_controller_class (Default: WP_REST_Posts_Controller ) – Specify a custom controller, which should be a subclass of WP_REST_Controller.

For example, I need to create a custom post type “Movie” with REST API support, my code will look something like this,

add_action( 'init', 'ccw_register_movies_post_type' );
function ccw_register_movies_post_type() {
	$labels = array(
	    'name'               => _x( 'Movies', 'post type general name', 'your-plugin-textdomain' ),
	    'singular_name'      => _x( 'Movie', 'post type singular name', 'your-plugin-textdomain' ),
	    'menu_name'          => _x( 'Movies', 'admin menu', 'your-plugin-textdomain' ),
	    'name_admin_bar'     => _x( 'Movie', 'add new on admin bar', 'your-plugin-textdomain' ),
	    'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
	    'add_new_item'       => __( 'Add New Movie', 'your-plugin-textdomain' ),
	    'new_item'           => __( 'New Movie', 'your-plugin-textdomain' ),
	    'edit_item'          => __( 'Edit Movie', 'your-plugin-textdomain' ),
	    'view_item'          => __( 'View Movie', 'your-plugin-textdomain' ),
	    'all_items'          => __( 'All Movies', 'your-plugin-textdomain' ),
	    'search_items'       => __( 'Search Movies', 'your-plugin-textdomain' ),
	    'parent_item_colon'  => __( 'Parent Movies:', 'your-plugin-textdomain' ),
	    'not_found'          => __( 'No movies found.', 'your-plugin-textdomain' ),
	    'not_found_in_trash' => __( 'No movies found in Trash.', 'your-plugin-textdomain' )
	);

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'movie' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'show_in_rest'       => true,
        'rest_base'          => 'movie',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
        'supports'           => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author', 'comments' )
    );
    register_post_type( 'movie', $args );
}

Adding REST API support to existing custom post type:

Sometimes you may want to add REST API support to a custom post type which is already registered, for example a custom post type which belongs to a theme. In this scenario, you can use global $wp_post_types to perform this task.

The example code for adding REST API support to an existing post type is as follows,

add_action( 'init', 'ccw_rest_apr_support_custom_post_type', 25 );
function ccw_rest_apr_support_custom_post_type() {
	global $wp_post_types;
	$post_type = 'movie';
	if( isset( $wp_post_types[ $post_type ] ) ) {
		$wp_post_types[$post_type]->show_in_rest = true;
		$wp_post_types[$post_type]->rest_base = $post_type;
		$wp_post_types[$post_type]->rest_controller_class = 'WP_REST_Posts_Controller';
	}
}

Read Also: How to Get Featured Image Thumbnail URL Using WP REST API?

0 Comments

Leave a Comment.