Sarmad Gardezi

Sarmad Gardezi

a freelance developer


Home about Portfolio Blog

How to Make Custom Post Type in WordPress

What is a Custom Post Type?

Custom post types are nothing more than a basic post but have different sets of parameters defined inside your code. The post type of regular posts is post, pages use page, attachments use attachment and so on. Since the release of WordPress 3.0, you can now create your own custom post types. If created successfully and the right parameters are returned you can have a customized area on your website like no other.

Custom Post Type UI

This plugin provides an easy to use interface to create and administer custom post types and taxonomies in WordPress. This plugin is created for WordPress 3.x. The plugin simply creates the types. You will need to add them to the theme yourself.

Adding Your Own Custom Post Type

Step 1 – Register Post Type

Open your functions.php file and add the code below to it. Make sure the code is added before the closing PHP tag. I will explain the code below.

// Creates Movie Reviews Custom Post Type
function movie_reviews_init() {
$args = array(
'label' => 'Movie Reviews',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'movie-reviews'),
'query_var' => true,
'menu_icon' => 'dashicons-video-alt',
'supports' => array(
'title',
'editor',
'excerpt',
'trackbacks',
'custom-fields',
'comments',
'revisions',
'thumbnail',
'author',
'page-attributes',)
);
register_post_type( 'movie-reviews', $args );
}
add_action( 'init', 'movie_reviews_init' );

Code Definitions

  • function movie_reviews_init() – Here we create a new function for our custom post type. We do this to limit conflicts with any other code inside our functions.php file. It is also a best practice when adding new code to any file like functions.php.
  • label – A plural descriptive name for the post type marked for translation. If you don’t declare a custom label, WordPress will use the name of the custom post type by default.
  • public – Whether a post type is intended to be used publicly either via the admin interface or by front-end users. WordPress sets this to false by default.Here we set it to true as we do what our custom post type to display publicly.
  • show_ui – Generates a default UI for managing this post type in the admin. You can set this to true or false. For the sake of usability, a UI in the admin area is always a good thing.
  • capability_type – Here we can declare what type of custom post type we will be dealing with. It is used to build the read, edit, and delete capabilities of a post or page. You can choose either post or page.
  • hierarchical – Whether the post type is hierarchical(e.g. page). Or in laymen’s terms, whether or not you can declare a parent page, child page, etc… of the post type. This is mainly intended for Pages. Here we declare it false so there’s no need to worry about it for our example.
  • rewrite – This rule is either true or false. The default is true so if slug argument is entered then the slug name is prepended to the posts. Our slug “movie-reviews” will be prepended to each new post of that type.
  • query_var – This rule is either true or false. It sets the post type name as a query variable.
  • menu_icon – This rule declares a custom icon for the admin area. Here we use a neat resource called dashicons that are included in WordPress already.
  • supports – This is usually an array of features the custom post type will support. Here we have quite a long list. These will tie into the admin area.
  • register_post_type($post_type, $args); – The register_post_type() is a function that WordPress recognizes as a custom post type generator. In this example it accepts two parameters which are the name of the post type itself and any arguments you would like to call.
  • add_action(‘init’, ‘movie_reviews_init’ ); – This line of code returns or calls our function so it fires and displays within our site.


RELATED POSTS

Video Tutorials