DEV Community

Cover image for Creating a WordPress Custom Post Type
Sarah Siqueira
Sarah Siqueira

Posted on • Updated on

Creating a WordPress Custom Post Type

By default, WordPress comes with five post types:

  • Post;

  • Page;

  • Attachment;

  • Revision;

  • Menu.

While working on a project built with WordPress, we may need to create our specific content types, for example, movies, books, series, pets, and kinds of flowers (if it's a project for flower shop e-commerce or whatever).

WordPress Custom Posts Types

Here is the magic of custom post types in WordPress, the type of 'posts' can be anything we want, and the limit is our imagination. We just need to create custom post types. That can be easily done with a few lines of code of plugins.

Registering new custom post types

There are amazing plugins available on the plugin repository that can help us with that, but, we also can do it with a few lines of code.
Once a custom post type is registered, it gets a new top-level administrative screen that can be used to manage and create posts of that type. To register a new post type, we should use the register_post_type() function.

Where the code goes?

The best practice, for creating a WordPress Custom Post Type is to create a plugin to register the custom post type we need for the project.
We also can add the code to the 'functions.php' file in our WordPress installation theme folder. But, unless we are using a child theme, the functions.php will override when we run some update of WordPress Core Files.

Plugin Code

First of all, we need start a plugin in the wp-content/plugins/new-plugin folder. In the new-plugin-folder create an index.php file.

// index.php 
<?php
    /*
    * Plugin Name: Custom Post Type Register
    * Description: This plugin register custom 
        * posts types for your project
    * Author: Sarah Siqueira
    *
        */
Enter fullscreen mode Exit fullscreen mode

It is a very simple plugin in fact. Above, yet inside the index.php file, paste the following code.
First, you will need to define the post type supports. The core default is an array containing 'title' and 'editor'.

/* Custom Post Type Start */ 
     function new_posttype_pet() {
     $supports = array(
       'title',
       'editor',
       'author', 
       'thumbnail', 
       'excerpt', 
       'custom-fields', // in case you want to enable 
support to custom fields
       'comments', 
       'revisions', 
       'post-formats', 
     );

Enter fullscreen mode Exit fullscreen mode

You also will need to define the labels and arguments for your post type. Be sure to replace all the words "pet/pets" with your new post type name. The post type name must not exceed 20 characters and may only contain lowercase alphanumeric characters, dashes, and underscores.

     $labels = array(
     'name' => _x('pets', 'plural'),
     'singular_name' => _x('pet', 'singular'),
     'menu_name' => _x('Pets', 'admin menu'),
     'name_admin_bar' => _x('Pet', 'admin bar'),
     'add_new' => _x('Add Pet', 'add new pet'),
     'add_new_item' => __('Add New Pet'),
     'new_item' => __('New pet', 'New Pet'),
     'edit_item' => __('Edit pet'),
     'view_item' => __('View pet'),
     'all_items' => __('All pets'),
     'search_items' => __('Search pets'),
     'not_found' => __('No pets found.'),
     );

     $args = array(
     'supports' => $supports,
     'labels' => $labels,
     'public' => true,
     'query_var' => true,
     'rewrite' => array('slug' => 'pets'),
     'has_archive' => true,
     'hierarchical' => false,
     );

Enter fullscreen mode Exit fullscreen mode

Call the register_post_typefunction:

register_post_type('pets', $args);
     }
Enter fullscreen mode Exit fullscreen mode

Finally, you will need a hook:

 add_action('init', 'new_posttype_pet');
Enter fullscreen mode Exit fullscreen mode

More information on official WordPress documentation.

Top comments (0)