SEOS THEMES

Free WordPress Themes
logo My Account
Buy All 31 Premium Themes For the Price Of €69.94

How to Create Custom Post Types in WordPress

WordPress houses lots of different types of content and they are divided into something called Post Types. A single item is called a post however this is also the name of a standard post type called posts. By default WordPress comes with a few different post types which are all stored in the database under the wp_posts table
The default post types that are always included within a WordPress installation unless otherwise removed are:

– Posts
– Pages
– Attachments
– Revisions
– Navigation Menus
– Custom CSS
– Changeset


// Custom post type function
function create_posttype_testimonials() {
register_post_type( 'testimonials',
// CPT Options
array(
    'labels' => array(
    'name' => __( 'Testimonials' ),
    'singular_name' => __( 'Testimonials' )
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'testimonials'),
    'show_in_rest' => true,
    ));
}
// Setup the function
add_action( 'init', 'create_posttype_testimonials' );

 



Accepted keys of the label array in the post type object:

name – General name for the post type, usually plural. The same and overridden by $post_type_object->label. Default is ‘Posts’ / ‘Pages’.
singular_name – Name for one object of this post type. Default is ‘Post’ / ‘Page’.
add_new – Default is ‘Add New’ for both hierarchical and non-hierarchical types. When internationalizing this string, please use a gettext context matching your post type. Example: _x( ‘Add New’, ‘product’, ‘textdomain’ );.
add_new_item – Label for adding a new singular item. Default is ‘Add New Post’ / ‘Add New Page’.
edit_item – Label for editing a singular item. Default is ‘Edit Post’ / ‘Edit Page’.
new_item – Label for the new item page title. Default is ‘New Post’ / ‘New Page’.
view_item – Label for viewing a singular item. Default is ‘View Post’ / ‘View Page’.
view_items – Label for viewing post type archives. Default is ‘View Posts’ / ‘View Pages’.
search_items – Label for searching plural items. Default is ‘Search Posts’ / ‘Search Pages’.
not_found – Label used when no items are found. Default is ‘No posts found’ / ‘No pages found’.
not_found_in_trash – Label used when no items are in the Trash. Default is ‘No posts found in Trash’ / ‘No pages found in Trash’.
parent_item_colon – Label used to prefix parents of hierarchical items. Not used on non-hierarchical post types. Default is ‘Parent Page:’.
all_items – Label to signify all items in a submenu link. Default is ‘All Posts’ / ‘All Pages’.
archives – Label for archives in nav menus. Default is ‘Post Archives’ / ‘Page Archives’.
attributes – Label for the attributes meta box. Default is ‘Post Attributes’ / ‘Page Attributes’.
insert_into_item – Label for the media frame button. Default is ‘Insert into post’ / ‘Insert into page’.
uploaded_to_this_item – Label for the media frame filter. Default is ‘Uploaded to this post’ / ‘Uploaded to this page’.
featured_image – Label for the featured image meta box title. Default is ‘Featured image’.
set_featured_image – Label for setting the featured image. Default is ‘Set featured image’.
remove_featured_image – Label for removing the featured image. Default is ‘Remove featured image’.
use_featured_image – Label in the media frame for using a featured image. Default is ‘Use as featured image’.
menu_name – Label for the menu name. Default is the same as name.
filter_items_list – Label for the table views hidden heading. Default is ‘Filter posts list’ / ‘Filter pages list’.
items_list_navigation – Label for the table pagination hidden heading. Default is ‘Posts list navigation’ / ‘Pages list navigation’.
items_list – Label for the table hidden heading. Default is ‘Posts list’ / ‘Pages list’.
item_published – Label used when an item is published. Default is ‘Post published.’ / ‘Page published.’
item_published_privately – Label used when an item is published with private visibility. Default is ‘Post published privately.’ / ‘Page published privately.’
item_reverted_to_draft – Label used when an item is switched to a draft. Default is ‘Post reverted to draft.’ / ‘Page reverted to draft.’
item_scheduled – Label used when an item is scheduled for publishing. Default is ‘Post scheduled.’ / ‘Page scheduled.’
item_updated – Label used when an item is updated. Default is ‘Post updated.’ / ‘Page updated.’


function get_post_type_labels( $post_type_object ) {
$nohier_vs_hier_defaults = array(
'name' => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
'singular_name' => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
'add_new' => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
'add_new_item' => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
'edit_item' => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
'new_item' => array( __( 'New Post' ), __( 'New Page' ) ),
'view_item' => array( __( 'View Post' ), __( 'View Page' ) ),
'view_items' => array( __( 'View Posts' ), __( 'View Pages' ) ),
'search_items' => array( __( 'Search Posts' ), __( 'Search Pages' ) ),
'not_found' => array( __( 'No posts found.' ), __( 'No pages found.' ) ),
'not_found_in_trash' => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ),
'parent_item_colon' => array( null, __( 'Parent Page:' ) ),
'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ),
'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
'attributes' => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
'featured_image' => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
'set_featured_image' => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
'remove_featured_image' => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
'use_featured_image' => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ),
'item_published' => array( __( 'Post published.' ), __( 'Page published.' ) ),
'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
'item_reverted_to_draft' => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
'item_scheduled' => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
'item_updated' => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
);
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
$post_type = $post_type_object->name;
$default_labels = clone $labels;
/**
* Filters the labels of a specific post type.
*
* The dynamic portion of the hook name, `$post_type`, refers to
* the post type slug.
*
* @since 3.5.0
*
* @see get_post_type_labels() for the full list of labels.
*
* @param object $labels Object with labels for the post type as member variables.
*/
$labels = apply_filters( "post_type_labels_{$post_type}", $labels );
// Ensure that the filtered labels contain all required default values.
$labels = (object) array_merge( (array) $default_labels, (array) $labels );
return $labels;
}

^