Sandeep Kumar Mishra
Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Custom post types & custom fields are widely used at our company and without these, we can’t imagine a website other than a blog. We don’t use any plugin to create custom post types because of following reasons.

This guide is about to create dynamic custom post types using procedure & OOP programming and we guess after reading it, you’ll never use any plugin because it’s super easy to do.
Procedure oriented programming is preferred if you’re creating a custom post type in theme. You can write it in functions.php located at your installed theme. Below are the steps to understand it easily.
add_action( 'init', 'weplugins_create_custom_post_type' );
function weplugins_create_custom_post_type() {
register_post_type( 'slides',
array(
'labels' => array(
'name' => __( 'Slides' ),
'singular_name' => __( 'Slide' )
),
'public' => true,
'has_archive' => true,
)
);
}
Note: If we’re creating a custom post type to save some private data, don’t make it ‘public’ => true and there’s no need for ‘has_archive’ => true if the custom post type is not public.
Object oriented programming is preferred if you’re creating a custom post type using a plugin. Below is the “weplugins_Custom_Posts” class which we use for our projects.
class weplugins_Custom_Posts {
function __construct($init) {
$this->settings = $init;
add_action( 'init', array(&$this, 'add_custom_post_type') );
}
function add_custom_post_type() {
register_post_type( $this->settings['slug'],
array(
'labels' => array(
'name' => __( $this->settings['name'] ),
'singular_name' => __( $this->settings['singular_name'] )
),
'public' => $this->settings['is_public'],
'has_archive' => $this->settings['has_archive'],
)
);
}
}
Now you need to just initialize this object with your new custom post settings. Below is example code to create a SLIDES custom post type.
$custom_posts = array(
"slug" => "slides",
"name" => "Slides",
"singular_name" => "Slide",
"is_public" => true,
"has_archive" => false,
);
$var = new weplugins_Custom_Posts($custom_posts);
Adding too many custom post types is not a good practice because that can affect your speed of database transactions. Some of the developers prefer a dedicated category of the posts instead of a new custom post type which is not a good idea. If you don’t want to make your custom post type public, I’d prefer a new custom table instead of a new custom post type.
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.