Guide to WordPress Custom Post Types

Guide to WordPress Custom Post Types

Custom Post Types are a new set of administrative options appearing along with the default post types such as PostsPagesAttachments etc. A Custom Post Type can store any type of information. It has a dedicated editor, media uploader and uses the existing WordPress table structure for ease in data management. The main advantage of creating custom post types using the WordPress API is that it equips itself well with existing themes and templates. Custom Post Types are also SEO friendly because of their nifty permalinks.

Why Use Custom Post Types?

Custom Post Types help us to keep different types of posts in different buckets. It separates our regular posts from others. Simple enough!

What Actually Are Custom Post Types?

Suppose you want your blog to have a separate section for Portfolio. By using Custom Post Types you can create a new type of item like Posts and Pages, which will contain a different set of data. It will have a new administration menu, dedicated editing pages, custom taxonomies and many more utilities required for full fledged publishing.

Let’s Create Custom Post Type “Portfolio” For Your New Portfolio Page

Register Custom Function :-

Add the below function to your theme’s function.php. The following line of code is used to execute the custom function named site_portfolio during the initialisation phase every time a page is generated.

Implement The Custom Function :-

The register_post_type function does most of the work for us. As soon as it is called it prepares the WordPress environment for a new custom post type including the different sections in the admin. This function takes two arguments: the first one is an unique name of the custom post type and the second one an array demonstrating the properties of the new custom post type. Here it’s another array containing the different labels, which indicates the text strings to be displayed in the different sections of the custom post type e.g. name displays the custom post type name in the dashboard, edit and view are displayed in Edit and View buttons respectively. I think the rest are pretty self explanatory.

In the next arguments:

  • 'public' => true determines the visibility of the custom post type both in the admin panel and front end.
  • 'menu_position' => 15 determines the menu position of the custom post type.
  • 'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ) determines the features of the custom post type which is to be displayed.
  • 'taxonomies' => array( '' ) creates custom taxonomies. Here it’s not defined.
  • 'menu_icon' => get_bloginfo( 'template_url').'/images/portfolio.png'  displays the admin menu icon.
  • 'has_archive' => true enables archiving of the custom post type.

Icon for Custom Post Type :-

Save a 16×16 pixel icon image in your current plugin folder. This is required for the custom post type icon in the dashboard.

Creating Meta Box Fields for Custom Post Types

The meta box mechanism uses the help of the built in WordPress meta box system and helps to add fields required specifically for the custom post types, without requiring the default custom fields in the editor.

Registering the Custom Function :-

Open your theme’s function.php and add the following function. This registers a function to be called when the WordPress admin interface is visited.

Implement The Custom Function For Meta Box :-

Here add_meta_box is the function used to add meta boxes to custom post types. Explanation of the given attributes:

  • portfolio_meta_box is the required HTML id attribute
  • Portfolio Details is the text visible in the heading of the meta box section
  • display_portfolio_meta_box is the callback which renders the contents of the meta box
  • portfolio is the name of the custom post type where the meta box will be displayed
  • normal defines the part of the page where the edit screen section should be shown
  • high defines the priority within the context where the boxes should show

Implementation of the display_portfolio_meta_box Function :-

This code renders the contents of the meta box. Here we have used an object variable that contains the information of each of the portfolio displayed in the editor. Using this object we have retrieved the post ID and used that to query the database to get the associated ‘Description’, ‘Type’ and ‘URL’ which in turn render the fields on the screen. When a new entry is added then the get_post_meta returns an empty string which results in displaying empty fields in the meta box.

Registering A Save Post Function :-

This function is executed when posts are saved or deleted from the admin panel. Here after checking for the type of received post data, if it is a Custom Post Type than it checks again to see if the meta box elements have been assigned values and then finally stores the values in those fields.

Creating a Custom Template Dedicated to Custom Post Types

  • archive-{post_type}.php
  • single-{post_type}.php
archive-{post_type}.php
If your custom post type were ‘product’, WordPress would look for archive-product.php to display the archive of posts.
single-{post_type}.php
If your custom post type were ‘product’, WordPress would look for single-product.php to display the single or permalink of the post.

If these files are not available in your Theme’s directory WordPress will look for archive.php and single.php, respectively. If even these files are not present it will default to index.php.

Create PHP file named archive-portfolio.php and put the following code in it. It will display all the post having post type “portfolio”.

Now for displaying the detail of particular post create single-portfolio.php

Now create a page in admin named as “Portfolio” (same name as post_type) and save it. Now access your page by your domain name/portfolio.

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu