Create a Custom Post Type on WordPress

Create a Custom Post Type on WordPress

In WordPress v3.0, one of the most notable updated features is the custom post type function. With this, developers can add various forms of content to their plugins and themes. This guide will demonstrate the benefits of and how to create a custom a custom post type on WordPress.

Let’s learn what a WordPress custom post type is exactly.

On the whole, a custom post type on WordPress represents a content form. Though it does have the term post in its name, it can represent a multitude of things. Here are a few examples of WordPress post types:

  1. Pages
  2. Posts
  3. FAQs
  4. Comments

When you create a custom post type on WordPress, you have a vast amount of control over how your posts are displayed. Do note, that this is a bit more complex than creating new categories or tags.

Image result for custom post type

Now we learn how to create a custom post type on WordPress.

The process of developing a custom WordPress post type is a pretty simple task. Best of all, WordPress does a great deal of the work for you. Firstly, copy/paste this code into the functions.php file of your theme.

 

//functions.php

add_action( 'init', 'create_post_type' ); 
function create_post_type() { 
register_post_type( 'sales reports', 
array( 
'labels' => array( 
'name' => __( 'Sales Reports' ), 
'singular_name' => __( 'Sales Reports' )
),
'public' => true, 
'has_archive' => true
 )
 );
 }

 

Basically, this simple code is all that we need to create a custom post type on WordPress. As shown above, we develop “sales reports” as a custom post type. With this, we will make a database for sales reports. This is a fairly primitive example. However, it correctly demonstrates how easy creating custom post types is. Going to your admin panel will now include “Sales Reports” as a menu item. Open the new section up and do some experimenting. WordPress generates every bit of the administrator interface, due to the setting ‘public’ => true.

 

Creating a Sales Report Database

Previously, we developed a simple WordPress custom post type. We got the job done. That being said, there is so much more functionality to achieve with custom post types.

Below, we develop a much more detailed and all-rounded custom post type:
// Create out post type


add_action( 'init', 'create_post_type' );
function create_post_type() {
$args = array(
'labels' => post_type_labels( 'Sales Reports' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('year',
'quarter',
'product',
'revenue',
'revenue forecast',
'comments'
)
);

register_post_type( 'sales report', $args );
}

// A helper function for generating the labels
function post_type_labels( $singular, $plural = '' )
{
if( $plural == '') $plural = $singular .'s';

return array(
'name' => _x( $plural, 'post type general name' ),
'singular_name' => _x( $singular, 'post type singular name' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New '. $singular ),
'edit_item' => __( 'Edit '. $singular ),
'new_item' => __( 'New '. $singular ),
'view_item' => __( 'View '. $singular ),
'search_items' => __( 'Search '. $plural ),
'not_found' => __( 'No '. $plural .' found' ),
'not_found_in_trash' => __( 'No '. $plural .' found in Trash' ),
'parent_item_colon' => ''
);
}

//Add filter ensuring that the text Sales Report, or sales report, is displayed when user updates a sales report


add_filter('post_updated_messages', 'post_type_updated_messages');
function post_type_updated_messages( $messages ) {
global $post, $post_ID;

$messages['sales report'] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __('Sales Report updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Sales Report updated.'),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf( __('Sales Rrport restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Sales Report published. <a href="%s">View sales report</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Sales Report saved.'),
8 => sprintf( __('Sales Report submitted. <a target="_blank" href="%s">Preview Sales Report</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
9 => sprintf( __('Sales Report scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview sales report</a>'),
// translators: Publish box date format, see php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __('Sales Report draft updated. <a target="_blank" href="%s">Preview sales report</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
);

return $messages;
}

In short, here we create a custom post type including the majority of possible option along with it. For further clarification, refer to the WordPress Codex to dwell into all of the options.

Viewing the Sales Reports

At this point, we have created a fine and dandy custom post type that can be managed as an admin.

Now, let us dwell into how to display these sales reports on your WordPress site.

Here, we describe two templates which can be used in your themes. With these, you can automatically display custom post content:

  1. single-{posttype}.php – We see posts on their own page as single.php. Similarly, custom posts can use single-{posttype.php} if applicable. Therefore, we can create a single-sales-report.php file. The posts will show according to this template.
  2. archive-{posttype}.php – WordPress v3.1 update brings us the ability to use the archive-type-template. Posts show on their own archives with archive.php. Likewise, we can utilize archive-{posttype}.php if applicable.

Of course, these template files come in real handy. However, at times, a bit more complexity comes in even handier. A custom query for additional control is what we are talking about. The good news is, with WP_Query we can set a specific post_type parameter.

$args = array( ‘post_type’ => ‘sales report’, ‘posts_per_page’ => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo ‘<div class=”entry-content”>’;
the_content();
echo ‘</div>’;
endwhile;

 

Tips when you Create Custom Post Types

Here are a few pointers to keep in mind:

  1. When developing custom post types, prefix the post type with a unique name to steer clear of conflicts with other themes and plugins. On the other hand, you must ensure that the length of your custom post type’s name does not exceed 20 characters.
  2. Refrain from using wp as the prefix for your post name. The reason being, this is only for internal WordPress names.
  3. Sometimes, you get a 404 error page when you initially try to see your custom post types. In this situation, do not sweat it. Go to Settings >> Permalinks. By doing so, your rewrite rules will be flushed. Be sure to flush the rewrite rules on your plugin activation if you are a plugin developer.

 

To conclude, now you have finished the process to create a custom post type on WordPress. To control this database, you can directly use your WordPress admin dashboard. Keep in mind, this guide is for beginners. In truth, there is a lot more that custom post types can achieve. They are one of WordPress’s most impactful features. With this in mind, get to carefully experimenting. Get the hang of it and uncover more custom post type functionality.

Leave a Reply

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