Register Custom Post Types in WordPress

For create a new custom post type in WordPress, the register_post_type() function will be use for this as shown here:

<?php register_post_type( $post_type, $args ); ?>

It accept the two parameters:

1. $post_type — First $post_type is a parameter. Remember that It should contain only lowercase letters, no spaces, and a max length of 20 characters.

2. $args — Second parameter $args is an array of arguments that define the post type and various options in WordPress.

Here is a basic example of registering a custom post type in WordPress. In WordPress there are two different places you can register a post type. First is in your theme’s functions.php file. And the second is in a custom plugin. To a custom plugin you could add the following code, in the below example, this code add to your theme’s functions.php file in WordPress and then see what happend.

<?php add_action( ‘init’, ‘prowp_register_my_new_post_types’ );

function prowp_register_my_new_post_types() {

register_post_type( ‘products’,array(‘labels’ => array(‘name’ => ‘New Products’),’public’ => true,));
}
?>

The WordPress admin panel dash board see now. Here you’ll noticed that a new menu called New Products has appeared just below Comments, as shown in Figure.

Register Custom Post Types in WordPress by Web Designer London
Register Custom Post Types in WordPress by Web Designer London

 

So this the custom post type you just registered with the preceding code that have you written. Another advantage is that, WordPress will automatically create the admin UI for your new custom post type.  New menu item it allows you to create new post type product entries as well as edit existing entries, it seems like posts and pages in WordPress CMS. Here is the basic example, but you can already tell the ease with which you can define custom content in WordPress.

NOTE When registering your custom post types then you should always use the init action hook.  After WordPress is fully initialized this is the first hook available and it will verify that your custom post type is registered early enough in the process of code.

When registering your custom post type in the WordPress there are many different arguments available. It’s important to understand these arguments to know what’s available.