Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

Creating Dynamic Custom Post Types in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
in Posts > Tech
August 9, 2022
5 minutes read
Creating Dynamic Custom Post Types in WordPress

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.
Premium Plugins Bundle

Premium Plugins Bundle

Unlock all our top WordPress plugins in one easy bundle — Easy to use, no coding required and dedicated support team.
Get the Bundle - Instant Access

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.

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.

    1. Step 1: Make sure WordPress recognizes your function on load so we use the “init” action to register our function.
      add_action( 'init', 'weplugins_create_custom_post_type' );
    2. Step 2: Now in the definition of the function ‘weplugins_create_custom_post_type’, we can register as many custom post types as we need.
      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.

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 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);

Conclusion

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.

Access Premium WordPress Plugins

Sandeep Kumar Mishra

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.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.