Jump to content

How to declare multiple custom post types in code block for REST API


Recommended Posts

I'm using this code successfully for one custom post type (CPT). I need to declare multiple CPT's. How do I code this?

 

Additional post types I want to declare:

bible3

worship-help

library-book

/**
  * Add REST API support to an already registered post type.
  */
  add_action( 'init', 'my_custom_post_type_rest_support', 25 );
  function my_custom_post_type_rest_support() {
  	global $wp_post_types;
  
  	//be sure to set this to the name of your post type!
  	$post_type_name = 'planet';
  	if( isset( $wp_post_types[ $post_type_name ] ) ) {
  		$wp_post_types[$post_type_name]->show_in_rest = true;
  		$wp_post_types[$post_type_name]->rest_base = $post_type_name;
  		$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
  	}
  
  }

Other than have a variable using that name, what the h... IS a 'custom post type'?

It's a wordpress post. Not $_POST. Here's the wordpress page on how to register post types.   https://codex.wordpress.org/Post_Types

Edited by taquitosensei

Use the register_post_type() function for any and all post types you're creating. Make sure 'show_in_rest' is set to true in the arguments to make the data available via the WP rest api.

Edited by maxxd
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.