How To Define The New Post Type?

147. How to define the new post type

There are really two options of adding a new type of content: create a new table, or use the existing posts table.

The posts table allows alternative post types. This with the fact that there are already a lot of functions that may be used instead of having to write your own made it a no-brainer for this exercise.

It can be very simple to add a different type of post.

Once all the form handling, and validation is removed this small function is what actually makes it happen:

function new_toot( $title , $content )  {
global $user_ID;

//get the relevent post vars and save away
// Create post object
$my_post = array();
$my_post[‘post_title’] = $title;
$my_post[‘post_content’] = $content;
$my_post[‘post_status’] = ‘publish’;
$my_post[‘post_date’] = date(‘Y-m-d H:i:s’);
$my_post[‘post_author’] = $user_ID;
$my_post[‘post_type’] = ‘toot’;
$my_post[‘post_category’] = array(0);

//Insert the post into the database
return wp_insert_post( $my_post );

All that really happens here is that an array is created here and passed it to wp_insert_post. That’s a function that already exists in WordPress. Deleting a post is way easier.

Managing this content means adding a new write panel and a new manage page that respectively pass commands to the plugin and trigger the add and delete functions.

This much, guys! Hope you liked this article!

Share this!

Leave a Comment

Your email address will not be published. Required fields are marked *