Creating custom post types can transform a typical WordPress website into a custom CMS (Content Management System) that fits the owner’s requirements perfectly. It’s true that not every webmaster needs to use them. However, once a need presents itself, it can improve the visuals, the user experience, and oftentimes, the SEO. Some examples include movie reviews, bookstores, eCommerce, mapping, or event booking websites. However, they can be used for a wide array of purposes, like this section, Code Snippets. It’s a custom post type if you haven’t noticed. Now that you have an idea, here’s how to make a custom post type using a code snippet in WordPress.
Usage
- Added to the root/wp-content/themes/theme-name/functions.php file. Not recommended. It will disappear during the next theme update, and you must make WordPress custom post type all over again.
- Added within the site-specific WordPress plugin to persevere through updates. Recommended option.
- Alternative to installing a third-party WordPress plugin that enable this functionality.
Code
function register_custom_post_type() {
$labels = array(
'name' => __( ‘Products’, ‘text_domain’ ),
'singular_name' => __( ‘Product’, ‘text_domain’ ),
'add_new' => __( ‘New Product’, ‘text_domain’ ),
'add_new_item' => __( ‘Add New Product’, ‘text_domain’ ),
'all_items' => __( ‘All Products’, ‘text_domain’ ),
'edit_item' => __( ‘Edit Product’, ‘text_domain’ ),
'new_item' => __( ‘New Product’, ‘text_domain’ ),
'view_item' => __( ‘View Product’, ‘text_domain’ ),
'update_item' => __( ‘Update Product’, ‘text_domain’ ),
'search_items' => __( ‘Search Products’, ‘text_domain’ ),
'not_found' => __( ‘No Products Found’, ‘text_domain’ ),
'items_list' => __( ‘Products List’, ‘text_domain’ ),
);
$supports = array(
'title',
'editor',
'excerpt',
'author',
'thumbnail',
'comments',
'trackbacks',
'revisions',
'page-attributes'
'post-formats'
);
$args= array(
'labels' => $labels,
'description' => 'Description text',
'supports' => $supports,
'taxonomies' => array( 'category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post'
);
register_post_type('product', $args);
}
add_action('init', 'register_custom_post_type');
Explanation
The numbers represent the code lines. Here’s some clarification:
- The function that contains your code. Change the function name to anything you want, as long as it doesn’t coincide with any other function you have.
- labels is an array e.g., a data structure that stores elements (key and value pairs) of an object, specifically, a custom post type (CPT). In this case, defines which labels that CPT should have. Those include the name of the CPT itself, its plural version, as well as the various pieces of texts that appear throughout the Admin Dashboard, defined in lines 6 through 15. Replace Product with the singular name of your CPT and Products with the plural form of your CPT.
- The name/plural label of your custom post type.
- The singular label of the CPT.
- 4 to 15 – The text_domain part enables internationalization, e.g., labels are translated to a local language for the user. Replace it with the name of your theme, if it supports it.
- 18 – supports is also an array that allows you to select which of the post features will be enabled (and displayed) for your custom post type. By default, only title and editor are enabled, but we enabled more of them. Delete the line for the feature you don’t want, or replace array with FALSE to disable any editing capabilities.
- 31 – args stands for arguments and is a variable that containing various arrays at once. You can change them from false to true, depending on if you need them or not. Here’s what many of them mean:
- description – Short description of your CPT.
- taxonomies – Define the taxonomy that applies to this CPT. We put category, but you can add a custom taxonomy name, if you registered it. If not, simply delete the whole line.
- hierarchical – If set to false, it will be identical to regular posts. If set to true, it will be like a Page, e.g., it can contain parent and child items.
- public – Visibility of your CPT to authors and visitors. We put true to enable it for both. You can restrict this with exclude_from_search and publicly_queryable.
- show_in_rest – The rest of the show arguments are self-explanatory. This one makes the CPT available to the REST API and the Gutenberg interface. It’s false by default, so remove it unless true.
- menu_position – The position in the WordPress Admin Section sidebar menu. We put 5 to make it appear below Posts. If you put 100, it will appear underneath Settings.
- has_archive – If set to true, you create rewrite rules. Your CPT gets an archive page at the URL follows your permalink settings. The WordPress slug becomes whatever you enter under register_post_type.
- 54 – You must hook your function to the init action hook. If you edited prefix in line 1, edit it here too.