Creating a custom post type in WordPress involves defining a new type of content that behaves similarly to built-in post types like “Posts” and “Pages.” Custom post types are useful when you want to manage and display different types of content on your website. Here’s a step-by-step guide on how to create a custom post type in WordPress:
- Choose a Code Location: Decide where you want to add the code to register your custom post type. You can add it to your theme’s
functions.php
file or create a custom plugin for this purpose. Using a custom plugin is recommended if you want to maintain the functionality even if you switch themes. - Add Code to Register the Custom Post Type: Open the chosen code location (e.g.,
functions.php
or your custom plugin file) and add the following code to register your custom post type. You can customize the labels, options, and arguments based on your requirements.function custom_post_type_registration() { $labels = array( 'name' => __('Custom Post Type'), 'singular_name' => __('Custom Post'), 'add_new' => __('Add New Custom Post'), 'add_new_item' => __('Add New Custom Post'), 'edit_item' => __('Edit Custom Post'), 'new_item' => __('New Custom Post'), 'view_item' => __('View Custom Post'), 'search_items' => __('Search Custom Posts'), 'not_found' => __('No custom posts found'), 'not_found_in_trash' => __('No custom posts found in trash'), 'parent_item_colon' => __('Parent Custom Post:') ); $args = array( 'labels' => $labels, 'public' => true, 'has_archive' => true, 'publicly_queryable' => true, 'query_var' => true, 'rewrite' => array('slug' => 'custom-post'), 'capability_type' => 'post', 'menu_icon' => 'dashicons-admin-post', // Use an icon from Dashicons 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'), ); register_post_type('custom_post', $args); } add_action('init', 'custom_post_type_registration');
- Save and Test: Save the changes to your code file. Visit your WordPress admin dashboard, and you should now see the new custom post type in the admin menu. You can add, edit, and manage custom posts just like you would with regular posts.
- Customize the Code: The code provided above is a basic example. You can customize various aspects of the custom post type, such as the labels, capabilities, and supported features. Refer to the WordPress Codex for the
register_post_type
function for more information on available arguments: register_post_type() – WordPress Codex.
Remember to replace
custom_post
with your desired post type name and adjust other settings as needed. Always make sure to backup your website before making changes to code files, especially if you’re new to coding or WordPress development.