How to create Custom Post Types in WordPress admin?

Now a days, WordPress is not just a blogging platform. From past few years, WordPress has become a robust content management system. By default, WordPress has two main post types:

  • Post
  • Pages

but you can create your own custom content types you want, these custom content types are referred as custom post types. We will show you how you can easily create your own custom post types.

What is custom post type?

WordPress has many post types like:

  • post
  • page
  • attachment
  • revision
  • menu

by default. But, sometimes you might be requiring some custom post types according to your requirements. For example, You may need to create a custom post type for products for an e-commerce website, for assignments for an e-learning website, or for movies for a review website.

How to create custom post types?

To create a custom post type, you can either create a new plugin or if you are working with themes you can paste the following code in Active theme of “functions.php” file.

 

First, we will walk you through with a fully working example of custom post type plugin so you understand how it works, take a look at below code. Create a new file in /wp-content/plugins/custom_post_types_plugin.php and paste the following code in it.

 

To create a new post type, we will use the register_post_type() function.

In “custom_post_type()” function we registers a post type with name of “php_book” with an array of arguments. These arguments are the options for our custom post type and has two parts, first array has label name of our custom post type and singular name for custom post. Second array include other arguments like public visibility, archive  and slug that will be used in URLs for this post type. You can learn more about using slug in WordPress here.

Now we need to activate our plugin from “Plugins” area.

Activate Plugin

 

After activating the Custom Post Type Plugin from plugins page you will see the new Custom Post Type with the name “Books” in admin area.

wordpress admin panel
wordpress admin panel

 

To add a new post you need to go to Book post from admin panel and click Add New.

Add New Book
Add New Book

 

You can see the entries of book types like this.

Books  - Custom Post Types
Books – Custom Post Types

 

Of course, you want to see the added books in front end of your site. If you will see the page in front end you will be having a 404 error, probably.

404 Page Not Found  -  Custom Post Types
404 Page Not Found – Custom Post Types

Why? Because, you have to rearrange the rewrite rules as you have added new route/slug for book type entries.

Go to your permalink settings, change them to default and then again to your desired URL structure. Now if you will open the page to view the added book, you will surely be seeing the correct page.

Archive Page  -  Custom Post Types
Archive Page – Custom Post Types

 

See the list of all of the arguments that can be passed to register_post_type.

0Shares

Related posts:

One thought on “How to create Custom Post Types in WordPress admin?”

Leave a Reply

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