Creating a custom post type in a Wordpress Plugin
So you have created your website in Wordpress and now you want to add more functionality to it other than just posts? Great, let’s do that. So I wanted to write this up because most people seem to put a lot of their heavy lifting in functions.php which is great in all but sometimes you don’t want a busy hard to read file there. So that is where including it in the plugin comes into play.

Initial setup for this is that I am using the boilerplate builder from here; https://wppb.me/ It is pretty light weight and I have had no trouble using it so far but it is a good starting block for a project.
First we need to call the function with the ‘init’ hook as you want it from the start. This is done in;
includes\class-plugin-name-plugin.php
In this file you’ll find this private function;
define_public_hooks(){
It is private because we don’t want it visible on the any part of the public api as these are mainly for the structure of the site.
So following the above code in the file we can start including our action.
// Custom Post Types$this->loader->add_action( 'init', $plugin_public, 'register_cpts' );
To break what we are doing here down;
We want to add_action()
as when the hook happens we want something else to happen.
init
Is where most of the Wordpress is loaded as well as user authentication.
$plugin_public
is getting the plugin name and version.
register_cpts
Is the function we will be making next where we the data for our custom post types are.
With that all done let us move to the next file;
public\class-demand-plugin-name-public.php
This is now where we want to now call that function;
public function register_cpts() {
Now in this example we’ll be registering a Podcast
// Post Type: Podcasts$labels = ["name" => "Podcasts","singular_name" => "Podcast","add_new_item" => "Add New Episode","edit_item" => "Edit Podcast Episode","new_item" => "New Episode","view_item" => "View Podcast Episode","view_items" => "View Podcast Episodes","search_items" => "Search Podcast Episode",];$args = ["label" => "Podcasts","labels" => $labels,"description" => "","public" => true,"publicly_queryable" => true,"show_ui" => true,"show_in_rest" => true,"rest_base" => "","rest_controller_class" => "WP_REST_Posts_Controller","has_archive" => true,"show_in_menu" => true,"show_in_nav_menus" => true,"delete_with_user" => false,"exclude_from_search" => false,"capability_type" => "post","map_meta_cap" => true,"hierarchical" => false,"rewrite" => [ "slug" => "podcast", "with_front" => true ],"query_var" => true,"menu_icon" => "dashicons-microphone","supports" => [ "title", "editor", "revisions" ],"taxonomies" => [ "category", "post_tag" ],];register_post_type( "podcast", $args );
And there we go we have our custom post type setup. Most of what you will want to include in the arrays is determined on your post type and what makes sense.

And here we can see the custom post type in the sidebar ready to get started.
Further reading on some of the wordpress functions used;
The codex and developer resources are always a good place to get started with this sort of thing. There is even generally comments that expand on some of them with voting so you can see what other people rate their contributions.