How to add a custom post type in Wordpress - WPTRINITY

How to add a custom post type in WordPress

In WordPress, we only get three default content types at the backend, i.e. posts, pages, and media. WordPress has become flexible and advanced. They provide an approach to adding more post types that have also diversified. The diversified usage demands more content types because posts, pages, and media are not enough and here is where WordPress custom post type comes in handy.

What is a WordPress Custom Post Type?

According to WordPress Codex, “Custom Post Types” also known as “Custom Content Types” is the specific type of post types that can be added to your WordPress using a simple function called the register_post_type(). The function allows you to add the new custom post type in accordance with a number of specifics such as supported features, availability, and labels.

Create a WordPress Custom Post Type

To create a custom post type for any particular theme on WordPress, navigate to function.php file from your WordPress theme directory then add the following code to it.

function my_custom_post_product() {
   $labels = array(
     'name'               => _x( 'Products', 'post type general name' ),
     'singular_name'      => _x( 'Product', 'post type singular name' ),
     'add_new'            => _x( 'Add New', 'book' ),
     'add_new_item'       => ( 'Add New Product' ),     
     'edit_item'          => ( 'Edit Product' ),
     'new_item'           => ( 'New Product' ),     
    'all_items'          => ( 'All Products' ),
     'view_item'          => ( 'View Product' ), 
    'search_items'       => ( 'Search Products' ),
     'not_found'          => ( 'No products found' ), 
    'not_found_in_trash' => ( 'No products found in the Trash' ), 
     'parent_item_colon'  => '',
     'menu_name'          => 'Products'
   );
   $args = array(
     'labels'        => $labels,
     'description'   => 'Holds our products and product specific data',
     'public'        => true,
     'menu_position' => 5,
     'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
     'has_archive'   => true,
   );
   register_post_type( 'product', $args ); 
 }
 add_action( 'init', 'my_custom_post_product' );
  • labels The labels option should be an array defining the different labels that a custom post type can have. I have separated this out above just to make the arguments for registering a post type clearer.
  • description A short explanation of our custom post type; what it does and why we’re using it.
  • public This option controls a bunch of things in one go. Setting this to true will set a bunch of other options (all to do with visibility) to true. For example, it is possible to have the custom post type visible but not queryable. More on this later.
  • menu_position Defines the position of the custom post type menu in the back end. Setting it to “5” places it below the “posts” menu; the higher you set it, the lower the menu will be placed.
  • supports This option sets up the default WordPress controls that are available in the edit screen for the custom post type. By default, only the title field and editor are shown. If you want to add support for comments, revisions, post formats and such you will need to specify them here. For a full list take a look at the arguments section in the Codex.
  • has_archive If set to true, rewrite rules will be created for you, enabling a post type archive at http://mysite.com/posttype/ (by default)

Leave a Comment

Your email address will not be published. Required fields are marked *