WordPress Custom Post Types (CPT)

What is WordPress Custom Post Types

WordPress Custom Post Types (CPT) is a way to create different types of content similar to Posts and Pages.

Suppose you want to create a News based website. You can either make use of default posts or create a new section called News using WordPress Custom Posts. Once you created Custom Post Types for ‘News’, You can add any number of News items in this section.

If you have used plugins, there are many who adds different custom post types in the Dashboard. For example, ‘WooCommerce’ adds custom post types for ‘products’. ‘Contact Form 7’ also create custom posts for ‘contact forms’.

In database, all post types are stored in ‘wp_posts’ table with a different ‘post_type’ value.

How to create WordPress Custom Posts:

WordPress Custom Post Types (CPT) is created using register_post_type() function. You can pass your post type name along with features you want to support.

For proper working, register_post_type() function should be called using ‘init‘ action.

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

You can specify a relevant but somewhat unique name for post types such as custom_news, brand_products, prefix_services to avoid conflict with other plugin’s custom post types. In second parameters, you pass an array of labels and other important parameters.

Example of WordPress Custom Post Types:

We can create WordPress custom post types using plugins as well as using custom code. In this tutorial, we will learn how to create WordPress custom post types using the code.

Important Points

  • The ‘rewrite’ parameter define SEO friendly slug in URL for each news.
  • The ‘supports’ parameters specify list of features available in these posts.
  • The ‘menu_position’ set the menu order for post type. See available menu positions
  • If parameter ‘public’ is true, custom posts will be visible in menu section and included in public search using other parameters ‘show_in_menu’ and ‘publicly_queryable’
  • The ‘labels’ parameter defines menu name and labels for different sections.

For more detail, visit WordPress Codex

Congratulations! Chapter Finished. Do you want to practice more?
Exercises & Assignments
  • No Content Found.
  • Interview FAQs
  • No Content Found.