WordPress Custom Post Types
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 the features you want to support.
For proper working, register_post_type() function should be called using ‘init‘ action.
Syntax:
<?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.
/* Starts: Register Custom Post Type for News */
function cpt_news_function() {
$labels = array(
'name' => _x( 'News', 'post type general name', 'textdomain'),
'singular_name' => _x( 'News', 'post type singular name', 'textdomain'),
'add_new' => _x( 'Add New', 'textdomain'),
'add_new_item' => __( 'Add New News', 'textdomain'),
'edit_item' => __( 'Edit News', 'textdomain'),
'new_item' => __( 'New News', 'textdomain'),
'all_items' => __( 'All News', 'textdomain'),
'view_item' => __( 'View News', 'textdomain'),
'search_items' => __( 'Search News', 'textdomain'),
'not_found' => __( 'No News found', 'textdomain'),
'not_found_in_trash' => __( 'No News found in the Trash','textdomain'),
'parent_item_colon' => '',
'menu_name' => 'News'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our News and News specific data',
'public' => true,
'menu_position' => 5,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => array('slug' => 'news'),
'supports' => array( 'title', 'editor', 'author','thumbnail','excerpt','custom-fields','revisions','comments'),
'has_archive' => true,
'show_in_rest' => true
);
register_post_type( 'my_news', $args );
}
add_action( 'init', 'cpt_news_function' );
Important Points
- The ‘rewrite’ parameter defines SEO friendly slug in URL for each news.
- The ‘supports’ parameters specify the list of features available in these posts.
- The ‘menu_position’ set the menu order for the post type. See available menu positions
- If parameter ‘public’ is true, custom posts will be visible in the menu section and included in public search using other parameters ‘show_in_menu’ and ‘publicly_queryable’
- The ‘labels’ parameter defines the menu names and labels for different sections.
For more detail, visit WordPress Codex
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |