Understanding and Creating a WordPress custom post type

Posted By : Harsh Soni | 30-Sep-2018

 

Wordpress is always taken a blog management system, but nowadays it's becoming popular for the web development. It provides us with a handful of content types by default like posts, pages, attachments, revisions, media, and navigation menus. Some of them work in the background and serve their purpose quietly and don't need much attention

 

Posts and pages are the most common thing from which user interacts. They are much similar and the only difference between them is that posts can be accessed from your wp-admin thought the post menu, while the pages can be accessed through the pages.

 

 

What is Custom Post Type?

 

A post type which is defined by you.  It is a content type – defined by you – which will appear in the wp-admin menus in the sidebar. And it will be similar to the one you were working with.

 

Creating a Custom Post Type?

 

Creating a custom Post type is easy and it can be done in two ways 

 

  1. Installing Custom Post type plugin
  2. Using custom code in the functions.php file 

 

Here I would prefer to go with the second option that is to add custom code to the functions.php file. As it provides more control over the functionality on the code level. So here is a sample code to add a custom post type "car_product".

 

function create_post_type() {
  register_post_type( 'car_product',
    array(
      'labels' => array(
        'name' => __( 'Cars' ),
        'singular_name' => __( 'Car' )
      ),
      'public' => true,
      'has_archive' => true,
    )
  );
}
add_action( 'init', 'create_post_type' );

 

It creates a post type named Car which is identified as car_product. The register_post_type() function receives two major arguments. The first one is labels and the second one is public which is a predefined flag to show the post type on the administration screens. You can understand more about creating Wordpress Custom Post type here.

 

About Author

Author Image
Harsh Soni

Harsh is an experienced software developer with a specialization in the MEAN stack. He is skilled in a wide range of web technologies, including Angular, Node.js, PHP, AWS, and Docker.Throughout his career, Harsh has demonstrated a strong commitment to delivering high-quality software solutions that meet the unique needs of his clients and organizations. His proficiency in Angular and Node.js has allowed him to build dynamic and interactive user interfaces, leveraging the power of modern front-end frameworks. Harsh's expertise also extends to cloud computing and infrastructure management using AWS, enabling him to design and deploy scalable applications with ease. Additionally, his knowledge of Docker has enabled him to streamline the development and deployment process, enhancing efficiency and reducing time-to-market. He excels at analyzing complex technical challenges and devising efficient strategies to overcome them, ensuring the successful completion of projects within deadlines.

Request for Proposal

Name is required

Comment is required

Sending message..