There are many way to create Custom Post Type in WordPress. By plugin or by some online CPT generator scripts like: https://wpturbo.dev/generators/post-type/
A very good article is written here. Refer this as well.
WordPress has idea of Custom Post Type & Custom plugins. Generally CPT is used to manage some different data in beside Posts & Pages. Those data can be managed by CRUD operations. Those CPT has various custom files like Dates, Custom fields etc. While Custom Plugins are used to change functionality of default WordPress.
As as senior WP guy you must have master in CPT & Custom plugins & of course Custom Theme.
So let’s start with CPT. There are many online tools which generates CPT. Also there are some plugin in WP library by which you can easily manage CPT from WP back-end. https://wordpress.org/plugins/custom-post-type-ui/
Here is the example how CPT created. Generally 2 main area. 1) Adding labels in form of array & 2) Adding arguments as array.
<?php
/*
* Plugin Name: Recipe Post Type by WPTurbo
* Description: This plugin adds a Recipe Post Type to your WordPress website. It was generated on <a href='https://wpturbo.dev'>WPTurbo</a> on June 25, 2024. Register an account to gather all your snippets and turbocharge your productivity.
* Version: 1.0.0
* Author: WPTurbo
* Author URI: https://wpturbo.dev
*/
/**
* Registers a custom post type 'recipes'.
*
* @since 1.0.0
*
* @return void
*/
function wpturbo_register_recipes_post_type() : void {
$labels = [
'name' => _x( 'Recipes', 'Post Type General Name', 'wpturbo-recipes-post-type' ),
'singular_name' => _x( 'Recipe', 'Post Type Singular Name', 'wpturbo-recipes-post-type' ),
'menu_name' => __( 'Recipes', 'wpturbo-recipes-post-type' ),
'name_admin_bar' => __( 'Recipes', 'wpturbo-recipes-post-type' ),
'archives' => __( 'Recipes Archives', 'wpturbo-recipes-post-type' ),
'attributes' => __( 'Recipes Attributes', 'wpturbo-recipes-post-type' ),
'parent_item_colon' => __( 'Parent Recipe:', 'wpturbo-recipes-post-type' ),
'all_items' => __( 'All Recipes', 'wpturbo-recipes-post-type' ),
'add_new_item' => __( 'Add New Recipe', 'wpturbo-recipes-post-type' ),
'add_new' => __( 'Add New', 'wpturbo-recipes-post-type' ),
'new_item' => __( 'New Recipe', 'wpturbo-recipes-post-type' ),
'edit_item' => __( 'Edit Recipe', 'wpturbo-recipes-post-type' ),
'update_item' => __( 'Update Recipe', 'wpturbo-recipes-post-type' ),
'view_item' => __( 'View Recipe', 'wpturbo-recipes-post-type' ),
'view_items' => __( 'View Recipes', 'wpturbo-recipes-post-type' ),
'search_items' => __( 'Search Recipes', 'wpturbo-recipes-post-type' ),
'not_found' => __( 'Recipe Not Found', 'wpturbo-recipes-post-type' ),
'not_found_in_trash' => __( 'Recipe Not Found in Trash', 'wpturbo-recipes-post-type' ),
'featured_image' => __( 'Featured Image', 'wpturbo-recipes-post-type' ),
'set_featured_image' => __( 'Set Featured Image', 'wpturbo-recipes-post-type' ),
'remove_featured_image' => __( 'Remove Featured Image', 'wpturbo-recipes-post-type' ),
'use_featured_image' => __( 'Use as Featured Image', 'wpturbo-recipes-post-type' ),
'insert_into_item' => __( 'Insert into Recipe', 'wpturbo-recipes-post-type' ),
'uploaded_to_this_item' => __( 'Uploaded to this Recipe', 'wpturbo-recipes-post-type' ),
'items_list' => __( 'Recipes List', 'wpturbo-recipes-post-type' ),
'items_list_navigation' => __( 'Recipes List Navigation', 'wpturbo-recipes-post-type' ),
'filter_items_list' => __( 'Filter Recipes List', 'wpturbo-recipes-post-type' ),
];
$labels = apply_filters( 'recipes-labels', $labels );
$args = [
'label' => __( 'Recipe', 'wpturbo-recipes-post-type' ),
'description' => __( 'The best recipes on the internet.', 'wpturbo-recipes-post-type' ),
'labels' => $labels,
'supports' => [
'title',
'excerpt',
'author',
'comments',
'revisions',
'custom-fields',
],
'taxonomies' => [
'category',
],
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-post',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'can_export' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'rest_base' => 'recipes',
'rest_controller_class' => 'WP_REST_recipes_Controller',
];
$args = apply_filters( 'recipes-args', $args );
register_post_type( 'recipes', $args );
}
add_action( 'init', 'wpturbo_register_recipes_post_type', 0 );
Not We can add CTP code in functions.php file but if Administrator will change theme, it will disappear. So We need to add this code in mu-plugin (Must Use Plugin) directory. No matter, which theme is activated, the code resides in mu-plugin will be always loaded.
Using register_post_type(), you can start working on CPT. Check code for more idea.
Also to manage template of CPT, we need to create new files in theme folder like, single-event.php & archive-event.php etc.
There are some important functions at this stage.
register_post_type() | Registers a post type. |
get_post_type_archive_link() | Retrieves the permalink for a post type archive. |
wp_trim_word() | Trims text to a certain number of words. |