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.

  • To avoid unnecessary dependencies on third party plugins.
  • Errors in the upcoming release of the plugin can affect our custom programming and might be bugs on the live site.
  • If client uninstalls this accidentally after a long period of development which happens sometimes.

This guides is about to create dynamic custom post types using procedure & oops programming and we guess after reading it, you’ll never use any plugin because it’s super easy to do.

Procedural Way to Create a Custom Post Types

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.

  • Step 1: Make sure WordPress recognize your function on load so we use “init” action to register our function.
    add_action( 'init', 'create_custom_post_type' );
  • Step 2: now in the definition of function ‘create_custom_post_type’, we can register as many as custom post type we need.
    function 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 custom post type to save some private data, don’t make it ‘public’=>true and not need for ‘has_archive’=>true if the custom post type is not public.

OOPS Way to Create a Custom Post Types

Object oriented programming is preferred if you’re creating a custom post type using a plugin. Below is “Custom_Posts” class which we use for our projects.

class 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 Custom_Posts ($custom_posts);

Conclusion

Adding too many custom post types is not a good practice because that’s can affect your speed of database transactions. Some of the developers prefer a dedicated category of the posts instead of a new custom post types which are 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.