How To Create Custom Post Types In WordPress

How To Create Custom Post Types In WordPress

I recently shared a couple of articles about Custom Post Types, but never really showed you how to create custom post types. Today I’ll show you how to easily create custom post types in WordPress. You can use this trick to create testimonials, portfolio, profiles & all.

Custom Post Types are just like Posts, Pages, Attachments etc. It will work in the same way as these things and it can store any type of information. bbPress also creates its forum as a Custom Post Type, and you can do it to. Plus, all data of your Custom Post Types will be added to the same database table as posts, pages etc.

Create Custom Post Type

We will create our Custom Post Type as a plugin. You can also create it in functions.php file, but I’d recommend you to create a custom plugin for this task.

[alert style=”green”]Easy Way Out: You can also simply generate Custom Post Types with generatewp.[/alert]

I’ll explain you how we created the Testimonials Page Type. First let’s create a perfect header for your custom plugin:

Before the closing PHP command, add the following action to our plugin:

Now, we will register and add elements to our Custom Post Type with following code:

The first part of your function is an unique name of our custom post type and the second part adds all the properties to your new post type.

And in the second part we have:

  • ‘public’ => true determines the visibility of your post type both in front and back-end.
  • ‘publicly_queryable’ => true determines whether users can query our custom post type.
  • ‘show_ui’ => true determines whether to show post type in the admin menu.
  • ‘exclude_from_search’ => true determines whether to exclude the post type from search results.
  • ‘query_var’ => true helps control the query_var used when a URL is loaded to be processed by parse_request().
  • ‘rewrite’ => true allows your custom post type to use SEO Friendly Permalinks.
  • ‘capability_type’ => ‘post’ controls the permissions for what a user with a specific role can do with the post type.
  • ‘has_archive’ => true enables archiving of the custom post type.
  • ‘hierarchical’ => false determines whether the post type is hierarchical.
  • ‘menu_position’ => 10 determines the menu position of the custom post type.
  • ‘supports’ => array( ‘editor’ ) determines the features of the custom post type which is to be displayed. You can display things like title, comments, custom-fields etc.
  • ‘taxonomies’ => array( ” ) creates custom taxonomies.
  • ‘menu_icon’ => plugins_url( ‘images/image.png’, __FILE__ ) displays the admin menu icon. To use, upload a 16×16 pixel icon image in your current plugin folder.

Save your plugin file. Now, just upload and activate this plugin to get started with your new Testimonials Post Type.

That’s it for this article. In following articles, I’ll show you to add custom meta boxes to your post types.