Tutorial: 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:
    Along the side of the WordPress dashboard options, hover to ‘Appearance’ then click on ‘Editor’. Once in the Theme Editor, find Theme Functions (functions.php) file in the list on the right of your screen and click on it to open it.
  2. Create a new function for your custom post type:
    Enter the following sample of code into your functions.php file, changing the name of the function (my_custom_posttype) to any other name if you wish. But do not yet update the file.
  3. function my_custom_posttype() {

    }

    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).

  4. Register the post type:
    Enter the following code sample (underlined) between the { } brackets:
  5. 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. ‘has_archive’ – true or false value to decide whether there should be support for an archive of posts of this type.
    b. ‘supports’ – add any attributes that you would like this custom post type to support.
    c. ‘taxonomies’ – add any taxonomies (such as ‘category’) that you would like the post type to support.

  6. include add_action command for post type function:
    Enter the following code sample (underlined) after the closing } bracket for the function:
  7. 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.

  8. Click Update File
  9. Your custom post type should display on the left-hand side menu in your Dashboard view

The complete sample code is as follows:

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

If you wish to download the sample code, click here
 
This was intended as a short tutorial but if you have any further questions, comments, feedback or queries please leave them below.

 
 

Comments

Leave a Reply

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