Create a custom post type in WordPress

Custom post types can be used in your WordPress setup to create customised content similar to regular posts but with some differences. With a few lines of PHP code it is very easy to create a custom post type without the use of a plugin as I will show you in this tutorial.

1. Open your theme’s functions.php file or your custom plugin’s core file

Access your theme or plugin files via SFTP or cPanel file manager. This is the preferable way to edit these files although you can through wp-admin also. If editing the theme’s functions.php file within your wp-admin dashboard. Move your cursor along the side of the WordPress dashboard options, and hover to ‘Appearance’ then click on ‘Editor’. Once in the Theme Editor, find the Theme Functions (functions.php) file in the list on the right of your screen and click on it to open it. If adding the custom taxonomy through a custom plugin hover over to ‘Plugins’ then select ‘Plugin Editor’. From the dropdown on the right hand side, select your plugin then click ‘Select’.

2. Create a new function for your custom post type

Enter the following sample of code into your file, changing the name of the function (create_new_posttype) to any other name if you wish. But do not yet update the file.

function create_new_posttype() {

}

3. Register the new post type

Enter the following code sample (underlined) between the { } brackets:

function my_custom_posttype() {
     register_post_type( ‘event’,
          array(
                  ‘labels’ => array(
                      ‘name’ => __( ‘Events’ ),
                      ‘singular_name’ => __( ‘Event’ )
          ),
          ‘has_archive’ => true,
          ‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’),
          ‘taxonomies’ => array( ‘category’ ),
          )
    );
}

As an example, here I am creating an ‘Event’ custom post type but this could easily be for a Product, Profile, Review, Story or anything you can imagine. The basic code is the same regardless.

You can change the following parameters under ‘Labels’. Other parameters in the function are optional but recommended:

a. ‘name’ – this is what the custom post type label is displayed as in the Dashboard left-hand side menu.
b. ‘singular_name’ – the name for one incidence of the custom post type (for example singular of pages is page, posts is post and events is event etc).
c. ‘has_archive’ – true or false value to decide whether there should be support for an archive of posts of this type.
d. ‘supports’ – add any attributes that you would like this custom post type to support.
e. ‘taxonomies’ – add any taxonomies (such as ‘category’) that you would like the post type to support.

4. Include add_action command for the function

Enter the following code sample (underlined) after the closing } bracket for the function:

function my_custom_posttype() {
     register_post_type( ‘event’,
          array(
               ‘labels’ => array(
               ‘name’ => __( ‘Events’ ),
               ‘singular_name’ => __( ‘Event’ )
          ),
          ‘has_archive’ => true,
          ‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’),
          ‘taxonomies’ => array( ‘category’ ),
          )
     );
}
add_action( ‘init’, ‘my_custom_posttype’ );

If you changed the name of the function earlier, you should use the same function name in the add_action command.

5. Click ‘Update File’ or upload/save changed file to the server

6. Your custom post type should display on the left-hand side menu in your Dashboard view

This was intended as a short tutorial but if you have any further questions, comments, feedback or queries please leave them as a comment below.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.